GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
AG_THREADS(3) FreeBSD Library Functions Manual AG_THREADS(3)

AG_Threadsagar threads support

#include <agar/core.h>

On platforms with threads support, Agar can be compiled with support for multithreading. In a threaded build, Agar API calls can be considered (safe to use from different threads without need for application-level synchronization) unless documented otherwise.

Even though calls are free-threaded, application-level synchronization (calls to AG_ObjectLock(3)) may still be needed in some cases. See EXAMPLES for some examples of thread-unsafe vs. thread-safe usages.

Agar function calls are free-threaded unless mentioned otherwise.

The AG_Object(3) system provides a per-object recursive mutex which is implicitely acquired before invoking object methods or processing events.

When compiled with threads support, Agar provides a portable, minimal interface to the operating system's native threads interface. These functions follow Agar's standard error-handling style (see AG_Intro(3)).

Mutexes (MUTual EXclusion devices) are commonly used to protect shared data structure against concurrent modifications.


void
(AG_Mutex *mutex);


int
(AG_Mutex *mutex);


void
(AG_Mutex *mutex);


int
(AG_Mutex *mutex);


void
(AG_Mutex *mutex);


void
(AG_Mutex *mutex);


int
(AG_Mutex *mutex);


void
(AG_Mutex *mutex);

The () function initializes a mutex structure. AG_MutexInitRecursive() initializes a recursive mutex (a mutex with a reference count), which allows nested AG_MutexLock() calls.

() frees all resources allocated for a mutex.

() and AG_MutexUnlock() respectively acquire and release a mutex.

() tries to acquire a mutex without blocking and immediately returns 0 on success. On failure, the function returns -1, but does not set any error message (so AG_GetError(3) should not be used).

void
AG_CondInit(AG_Cond *cv);


int
(AG_Cond *cv);


void
(AG_Cond *cv);


void
(AG_Cond *cv);


void
(AG_Cond *cv);


int
(AG_Cond *cv, AG_Mutex *m);


int
(AG_Cond *cv, AG_Mutex *m, const struct timespec *t);

() initializes a condition variable structure.

() releases resources allocated for a condition variable.

() unblock all threads which are currently blocked waiting on cv. AG_CondSignal() unblocks at least one thread currently blocked waiting on cv.

() blocks the calling thread until cv is signaled. The AG_CondTimedWait() variant will not block for more than the specified amount of time.

All of these functions will raise a fatal condition if an error is encountered.

void
(AG_Thread *th, void *(*fn)(void *arg), void *arg);


int
(AG_Thread *th, void *(*fn)(void *arg), void *arg);


void
(AG_Thread th);


int
(AG_Thread th);


void
(AG_Thread th, void **exitVal);


int
(AG_Thread th, void **exitVal);


void
(void *exitVal);


void
(AG_Thread th, int signal);


AG_Thread
(void);


int
(AG_Thread a, AG_Thread b);

() creates a new thread executing fn. The optional argument arg is passed to fn.

The () routine requests that the specified thread be cancelled. If the given thread is invalid, a fatal error is raised.

The () function suspends the execution of the current thread until th terminates. When it does, the value passed to AG_ThreadExit() is made available in exitVal.

() terminates the current thread. exitVal is an optional user pointer.

() sends a signal to the specified thread.

() returns the identifier of the current (caller's) thread. AG_ThreadEqual() returns 1 if the identifiers a and b both refer to the same thread, or 0 if they differ.

void
AG_ThreadKeyCreate(AG_ThreadKey *key, void (*destructor)(void *));


int
(AG_ThreadKey *key, void (*destructor)(void *));


void
(AG_ThreadKey key);


int
(AG_ThreadKey key);


void *
(AG_ThreadKey key);


void
(AG_ThreadKey key, const void *value);


int
(AG_ThreadKey key, const void *value);

() initializes a key (i.e., a handle) to a thread-specific value. The handle itself is accessible to all threads. The thread-specific value (i.e., the value specified by AG_ThreadKeySet(), and which defaults to NULL) will persist only for the life of the thread. If an optional destructor is given, that function will be called (with the thread-specific value as its argument), when the thread exists.

The () function releases resources allocated for a key.

() returns the thread-specific value associated with key.

() sets a thread-specific value with key.

The following code uses the return value of a VFS lookup in a manner which is thread-safe. A race condition exists between the AG_ObjectFind() call and the following access:

AG_Object *o;

o = AG_ObjectFind(root, "/Foo");
if (o != NULL) { /* ... */ }     /* UNSAFE access */

The following code accesses the returned object safely by acquiring the mutex of the VFS root object (which protects the entire VFS linkage):

AG_Object *o;

AG_ObjectLock(root);
o = AG_ObjectFind(root, "/Foo");
if (o != NULL) { /* ... */ }     /* Safe access */
AG_ObjectUnlock(root);

AG_Intro(3), AG_Object(3)

The AG_Threads interface first appeared in Agar 1.0

December 21, 2022 Agar 1.7

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.