ReactOS 0.4.16-dev-527-gdad3a09
Collaboration diagram for Semaphores:

Functions

err_t sys_sem_new (sys_sem_t *sem, u8_t count)
 
void sys_sem_signal (sys_sem_t *sem)
 
u32_t sys_arch_sem_wait (sys_sem_t *sem, u32_t timeout)
 
void sys_sem_free (sys_sem_t *sem)
 
int sys_sem_valid (sys_sem_t *sem)
 
void sys_sem_set_invalid (sys_sem_t *sem)
 

Detailed Description

Semaphores can be either counting or binary - lwIP works with both kinds. Semaphores are represented by the type "sys_sem_t" which is typedef'd in the sys_arch.h file. Mailboxes are equivalently represented by the type "sys_mbox_t". Mutexes are represented by the type "sys_mutex_t". lwIP does not place any restrictions on how these types are represented internally.

Function Documentation

◆ sys_arch_sem_wait()

u32_t sys_arch_sem_wait ( sys_sem_t sem,
u32_t  timeout 
)

Blocks the thread while waiting for the semaphore to be signaled. If the "timeout" argument is non-zero, the thread should only be blocked for the specified time (measured in milliseconds). If the "timeout" argument is zero, the thread should be blocked until the semaphore is signalled.

The return value is SYS_ARCH_TIMEOUT if the semaphore wasn't signaled within the specified time or any other value if it was signaled (with or without waiting). Notice that lwIP implements a function with a similar name, sys_sem_wait(), that uses the sys_arch_sem_wait() function.

Parameters
semthe semaphore to wait for
timeouttimeout in milliseconds to wait (0 = wait forever)
Returns
SYS_ARCH_TIMEOUT on timeout, any other value on success

Definition at line 86 of file sys_arch.c.

87{
88 LARGE_INTEGER LargeTimeout, PreWaitTime, PostWaitTime;
89 UINT64 TimeDiff;
91 PVOID WaitObjects[] = {&sem->Event, &TerminationEvent};
92
93 LargeTimeout.QuadPart = Int32x32To64(timeout, -10000);
94
95 KeQuerySystemTime(&PreWaitTime);
96
98 WaitObjects,
99 WaitAny,
100 Executive,
102 FALSE,
103 timeout != 0 ? &LargeTimeout : NULL,
104 NULL);
105 if (Status == STATUS_WAIT_0)
106 {
107 KeQuerySystemTime(&PostWaitTime);
108 TimeDiff = PostWaitTime.QuadPart - PreWaitTime.QuadPart;
109 TimeDiff /= 10000;
110
111 return TimeDiff;
112 }
113 else if (Status == STATUS_WAIT_1)
114 {
115 /* DON'T remove ourselves from the thread list! */
117
118 /* We should never get here! */
119 ASSERT(FALSE);
120
121 return 0;
122 }
123
124 return SYS_ARCH_TIMEOUT;
125}
unsigned long long UINT64
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
Status
Definition: gdiplustypes.h:25
KEVENT TerminationEvent
Definition: sys_arch.c:9
#define ASSERT(a)
Definition: mode.c:44
static HANDLE sem
Definition: sync.c:674
#define KernelMode
Definition: asm.h:38
#define Int32x32To64(a, b)
@ WaitAny
NTSTATUS NTAPI KeWaitForMultipleObjects(IN ULONG Count, IN PVOID Object[], IN WAIT_TYPE WaitType, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL, OUT PKWAIT_BLOCK WaitBlockArray OPTIONAL)
Definition: wait.c:586
NTSTATUS NTAPI PsTerminateSystemThread(IN NTSTATUS ExitStatus)
Definition: kill.c:1145
#define STATUS_WAIT_0
Definition: ntstatus.h:237
#define STATUS_WAIT_1
Definition: ntstatus.h:71
#define STATUS_SUCCESS
Definition: shellext.h:65
Definition: dhcpd.h:245
#define SYS_ARCH_TIMEOUT
Definition: sys.h:87
LONGLONG QuadPart
Definition: typedefs.h:114
@ Executive
Definition: ketypes.h:415

Referenced by sys_msleep(), tcpip_api_call(), tcpip_callback_wait(), and tcpip_send_msg_wait_sem().

◆ sys_sem_free()

void sys_sem_free ( sys_sem_t sem)

Deallocates a semaphore.

Parameters
semsemaphore to delete

Definition at line 72 of file sys_arch.c.

73{
74 /* No op (allocated in stack) */
75
77}
void sys_sem_set_invalid(sys_sem_t *sem)
Definition: sys_arch.c:66

Referenced by sys_msleep(), tcpip_api_call(), and tcpip_callback_wait().

◆ sys_sem_new()

err_t sys_sem_new ( sys_sem_t sem,
u8_t  count 
)

Create a new semaphore Creates a new semaphore. The semaphore is allocated to the memory that 'sem' points to (which can be both a pointer or the actual OS structure). The "count" argument specifies the initial state of the semaphore (which is either 0 or 1). If the semaphore has been created, ERR_OK should be returned. Returning any other error will provide a hint what went wrong, but except for assertions, no real error handling is implemented.

Parameters
sempointer to the semaphore to create
countinitial count of the semaphore
Returns
ERR_OK if successful, another err_t otherwise

Definition at line 46 of file sys_arch.c.

47{
48 ASSERT(count == 0 || count == 1);
49
50 /* It seems lwIP uses the semaphore implementation as either a completion event or a lock
51 * so I optimize for this case by using a synchronization event and setting its initial state
52 * to signalled for a lock and non-signalled for a completion event */
53
55
56 sem->Valid = 1;
57
58 return ERR_OK;
59}
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
GLuint GLuint GLsizei count
Definition: gl.h:1545
@ ERR_OK
Definition: err.h:55
@ SynchronizationEvent

Referenced by sys_msleep(), tcpip_api_call(), and tcpip_callback_wait().

◆ sys_sem_set_invalid()

void sys_sem_set_invalid ( sys_sem_t sem)

Invalidate a semaphore so that sys_sem_valid() returns 0. ATTENTION: This does NOT mean that the semaphore shall be deallocated: sys_sem_free() is always called before calling this function! This may also be a define, in which case the function is not prototyped.

Definition at line 66 of file sys_arch.c.

67{
68 sem->Valid = 0;
69}

Referenced by sys_sem_free().

◆ sys_sem_signal()

void sys_sem_signal ( sys_sem_t sem)

Signals a semaphore

Parameters
semthe semaphore to signal

Definition at line 80 of file sys_arch.c.

81{
83}
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by tcpip_thread_handle_msg().

◆ sys_sem_valid()

int sys_sem_valid ( sys_sem_t sem)

Returns 1 if the semaphore is valid, 0 if it is not valid. When using pointers, a simple way is to check the pointer for != NULL. When directly using OS structures, implementing this may be more complex. This may also be a define, in which case the function is not prototyped.

Definition at line 61 of file sys_arch.c.

62{
63 return sem->Valid;
64}