2 * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
8 * Defines the API in the
9 * <a href="group___pthread.html">Pthread library</a>
21 #include <sys/nacl_nice.h>
22 #include <sys/queue.h>
23 #include <sys/types.h>
26 * Signed 32-bit integer supporting CompareAndSwap and AtomicIncrement
27 * (see implementations), as well as atomic loads and stores.
28 * Instances must be naturally aligned.
30 typedef int AtomicInt32;
37 struct __nc_basic_thread_data;
39 /** Mutex type attributes */
41 /** Fast mutex type; for use with pthread_mutexattr_settype() */
42 PTHREAD_MUTEX_FAST_NP,
43 /** Recursive mutex type; for use with pthread_mutexattr_settype() */
44 PTHREAD_MUTEX_RECURSIVE_NP,
45 /** Error-checking mutex type; for use with pthread_mutexattr_settype() */
46 PTHREAD_MUTEX_ERRORCHECK_NP,
47 PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_FAST_NP,
48 PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
49 PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
51 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
55 * The layout of pthread_mutex_t and the static initializers are redefined
56 * in newlib's sys/lock.h file (including this file from sys/lock.h will
57 * cause include conflicts). When changing one of the definitions, make sure to
58 * change the second one.
61 * A structure representing a thread mutex. It should be considered an
62 * opaque record; the names of the fields can change anytime.
66 * mutex_state is either UNLOCKED (0), LOCKED_WITHOUT_WAITERS (1) or
67 * LOCKED_WITH_WAITERS (2). See "enum MutexState".
69 volatile int mutex_state;
73 * PTHREAD_MUTEX_FAST_NP, PTHREAD_MUTEX_RECURSIVE_NP,
74 * or PTHREAD_MUTEX_ERRORCHECK_NP
78 /** ID of the thread that owns the mutex */
79 struct __nc_basic_thread_data *owner_thread_id;
81 /** Recursion depth counter for recursive mutexes */
82 uint32_t recursion_counter;
85 * Padding is for compatibility with libraries (newlib etc.) that
86 * were built before libpthread switched to using futexes, and to
87 * match _LOCK_T in newlib's newlib/libc/include/sys/lock.h.
93 * A structure representing mutex attributes. It should be considered an
94 * opaque record and accessed only using pthread_mutexattr_settype().
99 * PTHREAD_MUTEX_FAST_NP, PTHREAD_MUTEX_RECURSIVE_NP,
100 * or PTHREAD_MUTEX_ERRORCHECK_NP
103 } pthread_mutexattr_t;
106 * A structure representing a condition variable. It should be considered an
107 * opaque record; the names of the fields can change anytime.
110 /* This is incremented on each pthread_cond_signal/broadcast() call. */
114 * Padding is for compatibility with libraries (newlib etc.) that
115 * were built before libpthread switched to using futexes.
121 * A structure representing condition variable attributes. Currently
122 * Native Client condition variables have no attributes.
125 int dummy; /**< Reserved; condition variables don't have attributes */
126 } pthread_condattr_t;
128 /** A value that represents an uninitialized handle. */
129 #define NC_INVALID_HANDLE -1
131 /** Maximum valid thread ID value. */
132 #define MAX_THREAD_ID (0xfffffffe)
134 /** Illegal thread ID value. */
135 #define NACL_PTHREAD_ILLEGAL_THREAD_ID ((pthread_t) 0)
137 /** Statically initializes a pthread_mutex_t representing a recursive mutex. */
138 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
139 {0, 1, NACL_PTHREAD_ILLEGAL_THREAD_ID, 0, NC_INVALID_HANDLE}
140 /** Statically initializes a pthread_mutex_t representing a fast mutex. */
141 #define PTHREAD_MUTEX_INITIALIZER \
142 {0, 0, NACL_PTHREAD_ILLEGAL_THREAD_ID, 0, NC_INVALID_HANDLE}
144 * Statically initializes a pthread_mutex_t representing an
145 * error-checking mutex.
147 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
148 {0, 2, NACL_PTHREAD_ILLEGAL_THREAD_ID, 0, NC_INVALID_HANDLE}
149 /** Statically initializes a condition variable (pthread_cond_t). */
150 #define PTHREAD_COND_INITIALIZER {0, NC_INVALID_HANDLE}
154 /* Functions for mutex handling. */
157 * Initializes a mutex using attributes in mutex_attr, or using the
158 * default values if the latter is NULL.
162 * @param mutex The address of the mutex structure to be initialized.
163 * @param mutex_attr The address of the attributes structure.
165 * @return 0 upon success, 1 otherwise
167 extern int pthread_mutex_init(pthread_mutex_t *mutex,
168 const pthread_mutexattr_t *mutex_attr);
175 * @param mutex The address of the mutex structure to be destroyed.
177 * @return 0 upon success, non-zero error code otherwise
179 extern int pthread_mutex_destroy(pthread_mutex_t *mutex);
182 * Tries to lock a mutex.
186 * @param mutex The address of the mutex structure to be locked.
188 * @return 0 upon success, EBUSY if the mutex is locked by another thread,
189 * non-zero error code otherwise.
191 extern int pthread_mutex_trylock(pthread_mutex_t *mutex);
198 * @param mutex The address of the mutex structure to be locked.
200 * @return 0 upon success, non-zero error code otherwise.
202 extern int pthread_mutex_lock(pthread_mutex_t *mutex);
204 /* Wait until lock becomes available, or specified time passes. */
205 extern int pthread_mutex_timedlock(pthread_mutex_t *mutex,
206 struct timespec *abstime);
213 * @param mutex The address of the mutex structure to be unlocked.
215 * @return 0 upon success, non-zero error code otherwise.
217 extern int pthread_mutex_unlock(pthread_mutex_t *mutex);
219 /* Mutex attributes manipulation */
222 * Initializes mutex attributes.
226 * @param attr The address of the attributes structure to be initialized.
230 extern int pthread_mutexattr_init(pthread_mutexattr_t *attr);
233 * Destroys mutex attributes structure.
237 * @param attr The address of the attributes structure to be destroyed.
241 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
244 * Sets the mutex type: fast, recursive or error-checking.
248 * @param attr The address of the attributes structure.
249 * @param kind PTHREAD_MUTEX_FAST_NP, PTHREAD_MUTEX_RECURSIVE_NP or
250 * PTHREAD_MUTEX_ERRORCHECK_NP.
252 * @return 0 on success, -1 for illegal values of kind.
254 extern int pthread_mutexattr_settype(pthread_mutexattr_t *attr,
258 * Gets the mutex type: fast, recursive or error-checking.
262 * @param attr The address of the attributes structure.
263 * @param kind Pointer to the location where the mutex kind value is copied.
267 extern int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr,
270 /* Functions for handling conditional variables. */
273 * Initializes a condition variable.
277 * @param cond Pointer to the condition variable structure.
278 * @param cond_attr Pointer to the attributes structure, should be NULL as
279 * Native Client does not support any attributes for a condition variable at
282 * @return 0 for success, 1 otherwise.
284 extern int pthread_cond_init(pthread_cond_t *cond,
285 const pthread_condattr_t *cond_attr);
288 * Destroys a condition variable.
292 * @param cond Pointer to the condition variable structure.
294 * @return 0 for success, non-zero error code otherwise.
296 extern int pthread_cond_destroy(pthread_cond_t *cond);
299 * Signals a condition variable, waking up one of the threads waiting on it.
303 * @param cond Pointer to the condition variable structure.
305 * @return 0 for success, non-zero error code otherwise.
307 extern int pthread_cond_signal(pthread_cond_t *cond);
310 * Wakes up all threads waiting on a condition variable.
314 * @param cond Pointer to the condition variable structure.
316 * @return 0 for success, non-zero error code otherwise.
318 extern int pthread_cond_broadcast(pthread_cond_t *cond);
321 * Waits for a condition variable to be signaled or broadcast.
325 * @param cond Pointer to the condition variable structure.
326 * @param mutex Pointer to the mutex structure. The mutex is assumed to be locked
327 * when this function is called.
329 * @return 0 for success, non-zero error code otherwise.
331 extern int pthread_cond_wait(pthread_cond_t *cond,
332 pthread_mutex_t *mutex);
335 * Waits for condition variable cond to be signaled or broadcast until
340 * @param cond Pointer to the condition variable structure.
341 * @param mutex Pointer to the mutex structure. The mutex is assumed to be locked
342 * when this function is called.
343 * @param abstime Absolute time specification; zero is the beginning of the epoch
344 * (00:00:00 GMT, January 1, 1970).
346 * @return 0 for success, non-zero error code otherwise.
348 int pthread_cond_timedwait_abs(pthread_cond_t *cond,
349 pthread_mutex_t *mutex,
350 const struct timespec *abstime);
353 * Waits for condition variable cond to be signaled or broadcast; wait time is
354 * limited by reltime.
358 * @param cond Pointer to the condition variable structure.
359 * @param mutex Pointer to the mutex structure. The mutex is assumed to be locked
360 * when this function is called.
361 * @param reltime Time specification, relative to the current time.
363 * @return 0 for success, non-zero error code otherwise.
365 int pthread_cond_timedwait_rel(pthread_cond_t *cond,
366 pthread_mutex_t *mutex,
367 const struct timespec *reltime);
370 * Defined for POSIX compatibility; pthread_cond_timedwait() is actually
371 * a macro calling pthread_cond_timedwait_abs().
373 #define pthread_cond_timedwait pthread_cond_timedwait_abs
377 /** Thread entry function type. */
378 typedef void *(*nc_thread_function)(void *p);
379 /** Thread identifier type. */
380 typedef struct __nc_basic_thread_data *pthread_t;
382 /** A structure representing thread attributes. */
384 int joinable; /**< 1 if the thread is joinable, 0 otherwise */
385 size_t stacksize; /**< The requested thread stack size in bytes. */
389 /** Joinable thread type; for use with pthread_attr_setdetachstate(). */
390 #define PTHREAD_CREATE_JOINABLE 1
391 /** Detached thread type; for use with pthread_attr_setdetachstate(). */
392 #define PTHREAD_CREATE_DETACHED 0
394 /** Minimum stack size; for use with pthread_attr_setstacksize(). */
395 #define PTHREAD_STACK_MIN (1024)
397 /* default stack size */
398 #define PTHREAD_STACK_DEFAULT (512 * 1024)
400 /* Thread functions */
407 * @param[out] thread_id A pointer to the location where the identifier of the
408 * newly created thread is stored on success.
409 * @param attr Thread attributes structure.
410 * @param start_routine Thread function.
411 * @param arg A single argument that is passed to the thread function.
413 * @return 0 for success, non-zero error code otherwise.
415 extern int pthread_create(pthread_t *thread_id,
416 const pthread_attr_t *attr,
417 void *(*start_routine)(void *p),
421 * Obtains the identifier of the current thread.
425 * @return Thread ID of the current thread.
427 extern pthread_t pthread_self(void);
430 * Compares two thread identifiers.
434 * @param thread1 Thread ID of thread A.
435 * @param thread2 Thread ID of thread B.
437 * @return 1 if both identifiers belong to the same thread, 0 otherwise.
439 extern int pthread_equal(pthread_t thread1, pthread_t thread2);
442 * Terminates the calling thread.
446 * @param retval Return value of the thread.
448 * @return The function never returns.
450 extern void pthread_exit(void *retval);
453 * Makes the calling thread wait for termination of another thread.
457 * @param th The identifier of the thread to wait for.
458 * @param thread_return If not NULL, points to the location where the return
459 * value of the terminated thread is stored upon completion.
461 * @return 0 on success, non-zero error code otherwise.
463 extern int pthread_join(pthread_t th, void **thread_return);
466 * Indicates that the specified thread is never to be joined with pthread_join().
467 * The resources of that thread will therefore be freed immediately when it
468 * terminates, instead of waiting for another thread to perform pthread_join()
473 * @param th Thread identifier.
475 * @return 0 on success, non-zero error code otherwise.
477 extern int pthread_detach(pthread_t th);
480 * Sends a signal to a thread. (Currently only a stub implementation.)
484 * @param thread_id The identifier of the thread to receive the signal.
485 * @param sig The signal value to send.
487 * @return 0 for success, non-zero error code otherwise.
489 extern int pthread_kill(pthread_t thread_id,
492 /* Functions for handling thread attributes. */
494 * Initializes thread attributes structure attr with default attributes
495 * (detachstate is PTHREAD_CREATE_JOINABLE).
499 * @param attr Pointer to thread attributes structure.
501 * @return 0 on success, non-zero error code otherwise.
503 extern int pthread_attr_init(pthread_attr_t *attr);
506 * Destroys a thread attributes structure.
510 * @param attr Pointer to thread attributes structure.
512 * @return 0 on success, non-zero error code otherwise.
514 extern int pthread_attr_destroy(pthread_attr_t *attr);
517 * Sets the detachstate attribute in thread attributes.
521 * @param attr Pointer to thread attributes structure.
522 * @param detachstate Value to be set, determines whether the thread is joinable.
524 * @return 0 on success, non-zero error code otherwise.
526 extern int pthread_attr_setdetachstate(pthread_attr_t *attr,
530 * Gets the detachstate attribute from thread attributes.
534 * @param attr Pointer to thread attributes structure.
535 * @param detachstate Location where the value of `detachstate` is stored upon
536 * successful completion.
538 * @return 0 on success, non-zero error code otherwise.
540 extern int pthread_attr_getdetachstate(pthread_attr_t *attr,
544 * Sets the stacksize attribute in thread attributes. Has no effect if the
545 * size is less than PTHREAD_STACK_MIN.
549 * @param attr Pointer to thread attributes structure.
550 * @param stacksize Value to be set, determines the minimum stack size.
552 * @return 0 on success, non-zero error code otherwise.
554 extern int pthread_attr_setstacksize(pthread_attr_t *attr,
558 * Gets the stacksize attribute in thread attributes.
562 * @param attr Pointer to thread attributes structure.
563 * @param stacksize Value to be set, determines the minimum stack size.
565 * @return 0 on success, non-zero error code otherwise.
567 extern int pthread_attr_getstacksize(pthread_attr_t *attr,
571 * Gets the maximum address of the stack (assuming stacks grow
572 * downwards) of the given thread.
574 * If the given thread exits concurrently with the call to this
575 * function, the behaviour is undefined.
577 * Note that in the future this may be removed and replaced with an
578 * implementation of pthread_getattr_np(), for consistency with Linux
579 * glibc. pthread_getattr_np() + pthread_attr_getstack() return the
580 * stack base (minimum) address and stack size. However, that is
581 * currently unimplementable under NaCl, because NaCl does not provide
582 * a way to determine the initial thread's stack size. See:
583 * https://code.google.com/p/nativeclient/issues/detail?id=3431
585 extern int pthread_get_stack_end_np(pthread_t tid, void **stack_end);
587 /* Functions for handling thread-specific data. */
589 /** Thread-specific key identifier type */
590 typedef int pthread_key_t;
592 /** Number of available keys for thread-specific data. */
593 #define PTHREAD_KEYS_MAX 512
596 * Creates a key value identifying a location in the thread-specific
601 * @param key Pointer to the location where the value of the key is stored upon
602 * successful completion.
603 * @param destr_function Pointer to a cleanup function that is called if the
604 * thread terminates while the key is still allocated.
606 * @return 0 on success, non-zero error code otherwise.
608 extern int pthread_key_create(pthread_key_t *key,
609 void (*destr_function)(void *p));
613 * Destroys a thread-specific data key.
617 * @param key Key value, previously obtained using pthread_key_create().
619 * @return 0 on success, non-zero error code otherwise.
621 extern int pthread_key_delete(pthread_key_t key);
624 * Stores a value in the thread-specific data slot identified by a key value.
628 * @param key Key value, previously obtained using pthread_key_create().
629 * @param pointer The value to be stored.
631 * @return 0 on success, non-zero error code otherwise.
633 extern int pthread_setspecific(pthread_key_t key,
634 const void *pointer);
637 * Gets the value currently stored at the thread-specific data slot
638 * identified by the key.
642 * @param key Key value, previously obtained using pthread_key_create().
644 * @return The value that was previously stored using pthread_setspecific is
645 * returned on success, otherwise NULL.
647 extern void *pthread_getspecific(pthread_key_t key);
650 * A structure describing a control block
651 * used with the pthread_once() function.
652 * It should be considered an opaque record;
653 * the names of the fields can change anytime.
656 /** A flag: 1 if the function was already called, 0 if it wasn't */
659 /** Synchronization lock for the flag */
660 pthread_mutex_t lock;
663 /** Static initializer for pthread_once_t. */
664 #define PTHREAD_ONCE_INIT {0, PTHREAD_MUTEX_INITIALIZER}
667 * Ensures that a piece of initialization code is executed at most once.
671 * @param __once_control Points to a static or extern variable statically
672 * initialized to PTHREAD_ONCE_INIT.
673 * @param __init_routine A pointer to the initialization function.
677 extern int pthread_once(pthread_once_t *__once_control,
678 void (*__init_routine)(void));
681 * Sets the scheduling priority of a thread.
685 * @param thread_id Identifies the thread to operate on.
686 * @param prio Scheduling priority to apply to that thread.
688 * @return 0 on success, non-zero error code otherwise.
690 extern int pthread_setschedprio(pthread_t thread_id, int prio);
693 * NOTE: this is only declared here to shut up
694 * some warning in the c++ system header files.
695 * We do not define this function anywhere.
698 extern int pthread_cancel(pthread_t th);
701 * NOTE: There are only stub implementations of these functions.
704 void pthread_cleanup_push(void (*func)(void *cleanup_arg), void *arg);
705 void pthread_cleanup_pop(int execute);
708 * @} End of PTHREAD group
715 #endif /* pthread.h */