Waitlist

Allows threads of a single process to wait for a certain condition.

If the condition is true a call to <trywakeup_waitlist> wakes up the first thread in the waiting list.  Before it is woken up its task arguments are set to the values given as arguments in the function <trywakeup_waitlist>.

Summary
WaitlistAllows threads of a single process to wait for a certain condition.
CopyrightThis program is free software.
Files
C-kern/api/platform/sync/waitlist.hHeader file of Waitlist.
C-kern/platform/Linux/sync/waitlist.cLinux specific implementation file Waitlist Linuximpl.
Types
struct waitlist_tExports waitlist_t.
Functions
test
unittest_platform_sync_waitlistTests waitlist functionality.
waitlist_tAllows threads of a single process to wait for a certain condition.
lastThe root pointer of the list of waiting threads.
nr_waitingThe number of threads waiting.
lockflagLock flag used to protect access to data members.
lifetime
waitlist_FREEStatic initializer.
init_waitlistInits a waiting list.
free_waitlistWakes up all waiting threads and frees all resources.
query
isempty_waitlistReturns true if no thread is waiting else false.
nrwaiting_waitlistReturns the number of threads waiting on this list.
synchronize
wait_waitlistSuspends the calling thread until some other calls trywakeup_waitlist.
trywakeup_waitlistTries to wake up the first waiting thread.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2011 Jörg Seebohn

Files

C-kern/api/platform/sync/waitlist.h

Header file of Waitlist.

C-kern/platform/Linux/sync/waitlist.c

Linux specific implementation file Waitlist Linuximpl.

Types

struct waitlist_t

typedef struct waitlist_t waitlist_t

Exports waitlist_t.

Functions

Summary
test
unittest_platform_sync_waitlistTests waitlist functionality.

test

unittest_platform_sync_waitlist

int unittest_platform_sync_waitlist(void)

Tests waitlist functionality.

waitlist_t

struct waitlist_t

Allows threads of a single process to wait for a certain condition.  Similar to semaphore_t.  The difference is that a thread’s »command« parameter is set to a specific value.  Therefore a woken up thread knows what to do next.

This object is thread safe.

Summary
lastThe root pointer of the list of waiting threads.
nr_waitingThe number of threads waiting.
lockflagLock flag used to protect access to data members.
lifetime
waitlist_FREEStatic initializer.
init_waitlistInits a waiting list.
free_waitlistWakes up all waiting threads and frees all resources.
query
isempty_waitlistReturns true if no thread is waiting else false.
nrwaiting_waitlistReturns the number of threads waiting on this list.
synchronize
wait_waitlistSuspends the calling thread until some other calls trywakeup_waitlist.
trywakeup_waitlistTries to wake up the first waiting thread.

last

struct slist_node_t * last

The root pointer of the list of waiting threads.

nr_waiting

size_t nr_waiting

The number of threads waiting.

lockflag

uint8_t lockflag

Lock flag used to protect access to data members.  Set and cleared with atomic operations.

lifetime

waitlist_FREE

#define waitlist_FREE { 0, 0, 0 }

Static initializer.  After initialization it is safe to call free_waitlist.

init_waitlist

int init_waitlist(/*out*/waitlist_t *wlist)

Inits a waiting list.  The waiting is protexted by a mutex.

free_waitlist

int free_waitlist(waitlist_t *wlist)

Wakes up all waiting threads and frees all resources.  Make sure that no more other thread is trying to call wait_waitlist if free_waitlist is processing or was already called.

query

isempty_waitlist

bool isempty_waitlist(waitlist_t *wlist)

Returns true if no thread is waiting else false.  Before testing the lock on the list is acquired.

But in case more than one thread calls trywakeup_waitlist you can not be sure that trywakeup_waitlist does not return EAGAIN even if isempty_waitlist returns false.

nrwaiting_waitlist

size_t nrwaiting_waitlist(waitlist_t *wlist)

Returns the number of threads waiting on this list.  Before testing the lock on the list is acquired.

But in case more than one thread calls trywakeup_waitlist you can not be sure that trywakeup_waitlist does not return EAGAIN even if nrwaiting_waitlist returns a value greater 0.

synchronize

wait_waitlist

int wait_waitlist(waitlist_t *wlist)

Suspends the calling thread until some other calls trywakeup_waitlist.  The waiting threads are woken up in FIFO order.  The calling thread enters itself as last in the waiting list and then it suspends itself.  See also <suspend_thread>.

trywakeup_waitlist

int trywakeup_waitlist(waitlist_t *wlist,
int (*main_task)(void * main_arg),
void *main_arg)

Tries to wake up the first waiting thread.  If the list is empty EAGAIN is returned and no error is logged.  If the list is not empty the first waiting thread gets its main_task and main_arg set.  Thie first thread is removed from the list and then resumed.

Allows threads of a single process to wait for a certain condition.
Implements Waitlist.
typedef struct waitlist_t waitlist_t
Exports waitlist_t.
struct waitlist_t
Allows threads of a single process to wait for a certain condition.
int unittest_platform_sync_waitlist(void)
Tests waitlist functionality.
struct slist_node_t * last
The root pointer of the list of waiting threads.
size_t nr_waiting
The number of threads waiting.
uint8_t lockflag
Lock flag used to protect access to data members.
#define waitlist_FREE { 0, 0, 0 }
Static initializer.
int init_waitlist(/*out*/waitlist_t *wlist)
Inits a waiting list.
int free_waitlist(waitlist_t *wlist)
Wakes up all waiting threads and frees all resources.
bool isempty_waitlist(waitlist_t *wlist)
Returns true if no thread is waiting else false.
size_t nrwaiting_waitlist(waitlist_t *wlist)
Returns the number of threads waiting on this list.
int wait_waitlist(waitlist_t *wlist)
Suspends the calling thread until some other calls trywakeup_waitlist.
int trywakeup_waitlist(waitlist_t *wlist,
int (*main_task)(void * main_arg),
void *main_arg)
Tries to wake up the first waiting thread.
Close