1 /* yarn.h -- generic interface for thread operations 2 * Copyright (C) 2008, 2011, 2012, 2015, 2018, 2019, 2020 Mark Adler 3 * Version 1.7 12 Apr 2020 Mark Adler 4 */ 5 6 /* 7 This software is provided 'as-is', without any express or implied 8 warranty. In no event will the author be held liable for any damages 9 arising from the use of this software. 10 11 Permission is granted to anyone to use this software for any purpose, 12 including commercial applications, and to alter it and redistribute it 13 freely, subject to the following restrictions: 14 15 1. The origin of this software must not be misrepresented; you must not 16 claim that you wrote the original software. If you use this software 17 in a product, an acknowledgment in the product documentation would be 18 appreciated but is not required. 19 2. Altered source versions must be plainly marked as such, and must not be 20 misrepresented as being the original software. 21 3. This notice may not be removed or altered from any source distribution. 22 23 Mark Adler 24 madler@alumni.caltech.edu 25 */ 26 27 /* Basic thread operations 28 29 This interface isolates the local operating system implementation of threads 30 from the application in order to facilitate platform independent use of 31 threads. All of the implementation details are deliberately hidden. 32 33 Assuming adequate system resources and proper use, none of these functions 34 can fail. As a result, any errors encountered will cause an exit() to be 35 executed, or the execution of your own optionally-provided abort function. 36 37 These functions allow the simple launching and joining of threads, and the 38 locking of objects and synchronization of changes of objects. The latter is 39 implemented with a single lock type that contains an integer value. The 40 value can be ignored for simple exclusive access to an object, or the value 41 can be used to signal and wait for changes to an object. 42 43 -- Arguments -- 44 45 thread *thread; identifier for launched thread, used by join 46 void probe(void *); pointer to function "probe", run when thread starts 47 void *payload; single argument passed to the probe function 48 lock *lock; a lock with a value -- used for exclusive access to 49 an object and to synchronize threads waiting for 50 changes to an object 51 long val; value to set lock, increment lock, or wait for 52 int n; number of threads joined 53 54 -- Thread functions -- 55 56 thread = launch(probe, payload) - launch a thread -- exit via probe() return 57 join(thread) - join a thread and by joining end it, waiting for the thread 58 to exit if it hasn't already -- will free the resources allocated by 59 launch() (don't try to join the same thread more than once) 60 n = join_all() - join all threads launched by launch() that are not joined 61 yet and free the resources allocated by the launches, usually to clean 62 up when the thread processing is done -- join_all() returns an int with 63 the count of the number of threads joined (join_all() should only be 64 called from the main thread, and should only be called after any calls 65 of join() have completed) 66 67 -- Lock functions -- 68 69 lock = new_lock(val) - create a new lock with initial value val (lock is 70 created in the released state) 71 possess(lock) - acquire exclusive possession of a lock, waiting if necessary 72 twist(lock, [TO | BY], val) - set lock to or increment lock by val, signal 73 all threads waiting on this lock and then release the lock -- must 74 possess the lock before calling (twist releases, so don't do a 75 release() after a twist() on the same lock) 76 wait_for(lock, [TO_BE | NOT_TO_BE | TO_BE_MORE_THAN | TO_BE_LESS_THAN], val) 77 - wait on lock value to be, not to be, be greater than, or be less than 78 val -- must possess the lock before calling, will possess the lock on 79 return but the lock is released while waiting to permit other threads 80 to use twist() to change the value and signal the change (so make sure 81 that the object is in a usable state when waiting) 82 release(lock) - release a possessed lock (do not try to release a lock that 83 the current thread does not possess) 84 val = peek_lock(lock) - return the value of the lock (assumes that lock is 85 already possessed, no possess or release is done by peek_lock()) 86 free_lock(lock) - free the resources allocated by new_lock() (application 87 must assure that the lock is released before calling free_lock()) 88 89 -- Memory allocation --- 90 91 yarn_mem(better_malloc, better_free) - set the memory allocation and free 92 routines for use by the yarn routines where the supplied routines have 93 the same interface and operation as malloc() and free(), and may be 94 provided in order to supply thread-safe memory allocation routines or 95 for any other reason -- by default malloc() and free() will be used 96 97 -- Error control -- 98 99 yarn_prefix - a char pointer to a string that will be the prefix for any 100 error messages that these routines generate before exiting -- if not 101 changed by the application, "yarn" will be used 102 yarn_abort - an external function that will be executed when there is an 103 internal yarn error, due to out of memory or misuse -- this function 104 may exit to abort the application, or if it returns, the yarn error 105 handler will exit (set to NULL by default for no action) 106 */ 107 108 extern char *yarn_prefix; 109 extern void (*yarn_abort)(int); 110 111 void yarn_mem(void *(*)(size_t), void (*)(void *)); 112 113 typedef struct thread_s thread; 114 thread *launch_(void (*)(void *), void *, char const *, long); 115 #define launch(a, b) launch_(a, b, __FILE__, __LINE__) 116 void join_(thread *, char const *, long); 117 #define join(a) join_(a, __FILE__, __LINE__) 118 int join_all_(char const *, long); 119 #define join_all() join_all_(__FILE__, __LINE__) 120 121 typedef struct lock_s lock; 122 lock *new_lock_(long, char const *, long); 123 #define new_lock(a) new_lock_(a, __FILE__, __LINE__) 124 void possess_(lock *, char const *, long); 125 #define possess(a) possess_(a, __FILE__, __LINE__) 126 void release_(lock *, char const *, long); 127 #define release(a) release_(a, __FILE__, __LINE__) 128 enum twist_op { TO, BY }; 129 void twist_(lock *, enum twist_op, long, char const *, long); 130 #define twist(a, b, c) twist_(a, b, c, __FILE__, __LINE__) 131 enum wait_op { 132 TO_BE, /* or */ NOT_TO_BE, /* that is the question */ 133 TO_BE_MORE_THAN, TO_BE_LESS_THAN }; 134 void wait_for_(lock *, enum wait_op, long, char const *, long); 135 #define wait_for(a, b, c) wait_for_(a, b, c, __FILE__, __LINE__) 136 long peek_lock(lock *); 137 void free_lock_(lock *, char const *, long); 138 #define free_lock(a) free_lock_(a, __FILE__, __LINE__) 139