|
28 | 28 | #include <stdint.h> |
29 | 29 | #include <stddef.h> |
30 | 30 | #include <stdbool.h> |
31 | | - |
32 | 31 | // Forward declarations to break circular dependencies |
33 | 32 | typedef enum ptk_err ptk_err; |
| 33 | +typedef struct ptk_shared_handle ptk_shared_handle_t; |
34 | 34 | typedef int64_t ptk_time_ms; |
35 | 35 |
|
| 36 | +// Timeout constants (from ptk_utils.h) |
| 37 | +#define PTK_TIME_WAIT_FOREVER (INT64_MAX) |
| 38 | +#define PTK_TIME_NO_WAIT (INT64_MIN) |
| 39 | + |
36 | 40 | #if defined(_WIN32) && defined(_MSC_VER) |
37 | 41 | #define ptk_thread_local __declspec(thread) |
38 | 42 | #else |
39 | 43 | #define ptk_thread_local __thread |
40 | 44 | #endif |
41 | 45 |
|
42 | 46 |
|
| 47 | +/* Mutex and condition variable types removed from public API. |
| 48 | + * Use shared memory handles for synchronization instead: |
| 49 | + * - Data protection: use_shared() macro provides automatic synchronization |
| 50 | + * - Thread communication: ptk_thread_signal() and ptk_thread_wait() |
| 51 | + * - OS-native mutexes used internally by shared memory implementation only |
| 52 | + */ |
| 53 | + |
43 | 54 | /** |
44 | | - * @brief Forward declaration of a mutex type. |
| 55 | + * @brief Thread handle type (uses shared memory for safe cross-thread access) |
45 | 56 | */ |
46 | | -typedef struct ptk_mutex ptk_mutex; |
| 57 | +typedef ptk_shared_handle_t ptk_thread_handle_t; |
47 | 58 |
|
48 | 59 | /** |
49 | | - * @brief Forward declaration of a condition variable type. |
| 60 | + * @brief No parent constant for root threads |
50 | 61 | */ |
51 | | -typedef struct ptk_cond_var ptk_cond_var; |
| 62 | +#define PTK_THREAD_NO_PARENT PTK_SHARED_INVALID_HANDLE |
52 | 63 |
|
53 | 64 | /** |
54 | | - * @brief Forward declaration of a thread type. |
| 65 | + * @brief Thread signal types for unified signaling API |
| 66 | + */ |
| 67 | +typedef enum { |
| 68 | + PTK_THREAD_SIGNAL_WAKEUP, // General wake-up signal |
| 69 | + PTK_THREAD_SIGNAL_ABORT, // Request graceful shutdown |
| 70 | + PTK_THREAD_SIGNAL_TERMINATE, // Force immediate termination |
| 71 | + PTK_THREAD_SIGNAL_CHILD_DIED // Child death notification (automatic) |
| 72 | +} ptk_thread_signal_t; |
| 73 | + |
| 74 | +/* Public mutex and condition variable APIs removed. |
| 75 | + * Synchronization handled by shared memory system: |
| 76 | + * |
| 77 | + * Instead of mutexes, use shared memory access patterns: |
| 78 | + * use_shared(data_handle, timeout, my_data_t *data) { |
| 79 | + * // Automatically synchronized access to data |
| 80 | + * } on_shared_fail { |
| 81 | + * // Handle timeout or failure |
| 82 | + * } |
| 83 | + * |
| 84 | + * Instead of condition variables, use thread signals: |
| 85 | + * ptk_thread_signal(worker_thread); // Wake up waiting thread |
| 86 | + * ptk_thread_wait(handle, timeout); // Wait for signal |
55 | 87 | */ |
56 | | -typedef struct ptk_thread ptk_thread; |
57 | 88 |
|
58 | 89 | //============================ |
59 | | -// Mutex Functions |
| 90 | +// Thread Functions |
60 | 91 | //============================ |
61 | 92 |
|
62 | 93 | /** |
63 | | - * @brief Creates a new recursive mutex. |
| 94 | + * @brief Function type for thread entry points. |
64 | 95 | * |
65 | | - * @param allocator The allocator to use for creating the mutex |
66 | | - * @return Pointer to the newly created mutex, or NULL on failure |
| 96 | + * @param data Shared memory handle containing user data for the thread. |
67 | 97 | */ |
68 | | -extern ptk_mutex *ptk_mutex_create(void); |
| 98 | +typedef void (*ptk_thread_func)(ptk_shared_handle_t data); |
69 | 99 |
|
70 | 100 | /** |
71 | | - * @brief Attempts to lock the mutex, optionally waiting for a timeout. |
| 101 | + * @brief Creates and starts a new thread with parent-child relationship. |
| 102 | + * |
| 103 | + * Child threads automatically register with parent and signal parent when they die. |
| 104 | + * Use PTK_THREAD_NO_PARENT for root threads. |
72 | 105 | * |
73 | | - * @param mutex Pointer to the mutex. |
74 | | - * @param timeout_ms Timeout in milliseconds. Use PTK_TIME_NO_WAIT or PTK_TIME_WAIT_FOREVER for special cases. |
75 | | - * @return Error code. |
| 106 | + * @param parent Parent thread handle, or PTK_THREAD_NO_PARENT for root threads |
| 107 | + * @param func Entry point for the thread function |
| 108 | + * @param data User data to pass to the thread function (shared memory handle) |
| 109 | + * @return Thread handle for the new thread, or PTK_SHARED_INVALID_HANDLE on failure |
76 | 110 | */ |
77 | | -extern ptk_err ptk_mutex_wait_lock(ptk_mutex *mutex, ptk_time_ms timeout_ms); |
| 111 | +extern ptk_thread_handle_t ptk_thread_create(ptk_thread_handle_t parent, |
| 112 | + ptk_thread_func func, |
| 113 | + ptk_shared_handle_t data); |
78 | 114 |
|
79 | 115 | /** |
80 | | - * @brief Unlocks the previously locked mutex. |
| 116 | + * @brief Get the current thread's handle. |
81 | 117 | * |
82 | | - * @param mutex Pointer to the mutex. |
83 | | - * @return Error code. |
| 118 | + * @return Handle for the calling thread |
84 | 119 | */ |
85 | | -extern ptk_err ptk_mutex_unlock(ptk_mutex *mutex); |
86 | | - |
87 | | -//============================ |
88 | | -// Condition Variable Functions |
89 | | -//============================ |
| 120 | +extern ptk_thread_handle_t ptk_thread_self(void); |
90 | 121 |
|
91 | 122 | /** |
92 | | - * @brief Creates a condition variable. |
| 123 | + * @brief Wait for signals or timeout (calling thread waits for itself). |
93 | 124 | * |
94 | | - * @param allocator The allocator to use for creating the condition variable |
95 | | - * @return Pointer to the created condition variable, or NULL on failure |
| 125 | + * The calling thread waits until signaled or timeout occurs. Socket operations |
| 126 | + * (like ptk_tcp_accept) also wake up on any signal for responsive server loops. |
| 127 | + * |
| 128 | + * @param timeout_ms Timeout in milliseconds |
| 129 | + * @return PTK_OK on timeout, PTK_SIGNAL if signaled (check ptk_thread_get_last_signal()) |
96 | 130 | */ |
97 | | -extern ptk_cond_var *ptk_cond_var_create(void); |
| 131 | +extern ptk_err ptk_thread_wait(ptk_time_ms timeout_ms); |
98 | 132 |
|
99 | 133 | /** |
100 | | - * @brief Signals the condition variable, waking one waiting thread. |
| 134 | + * @brief Send a signal to a thread. |
| 135 | + * |
| 136 | + * Signals cause the target thread to wake up from ptk_thread_wait() or socket |
| 137 | + * operations like ptk_tcp_accept(). The thread can check the signal type. |
101 | 138 | * |
102 | | - * @param cond_var Pointer to the condition variable. |
103 | | - * @return Error code. |
| 139 | + * @param handle Thread handle to signal |
| 140 | + * @param signal_type Type of signal to send |
| 141 | + * @return Error code |
104 | 142 | */ |
105 | | -extern ptk_err ptk_cond_var_signal(ptk_cond_var *cond_var); |
| 143 | +extern ptk_err ptk_thread_signal(ptk_thread_handle_t handle, ptk_thread_signal_t signal_type); |
106 | 144 |
|
107 | 145 | /** |
108 | | - * @brief Waits for the condition variable to be signaled. |
| 146 | + * @brief Get the last signal received by the calling thread. |
| 147 | + * |
| 148 | + * Call this after ptk_thread_wait() returns PTK_SIGNAL to determine what signal was received. |
109 | 149 | * |
110 | | - * @param cond_var Pointer to the condition variable. |
111 | | - * @param mutex Pointer to an already locked mutex that will be released during the wait. |
112 | | - * @param timeout_ms Timeout in milliseconds. Use THREAD_NO_WAIT or THREAD_WAIT_FOREVER for special cases. |
113 | | - * @return Error code. |
| 150 | + * @return The last signal type received, or PTK_THREAD_SIGNAL_WAKEUP if none |
114 | 151 | */ |
115 | | -extern ptk_err ptk_cond_var_wait(ptk_cond_var *cond_var, ptk_mutex *mutex, ptk_time_ms timeout_ms); |
| 152 | +extern ptk_thread_signal_t ptk_thread_get_last_signal(void); |
116 | 153 |
|
117 | 154 | //============================ |
118 | | -// Thread Functions |
| 155 | +// Parent-Child Thread Management |
119 | 156 | //============================ |
120 | 157 |
|
121 | 158 | /** |
122 | | - * @brief Function type for thread entry points. |
| 159 | + * @brief Get the parent thread handle. |
| 160 | + * |
| 161 | + * @param thread Thread handle to query (use ptk_thread_self() for current thread) |
| 162 | + * @return Parent thread handle, or PTK_THREAD_NO_PARENT if no parent |
| 163 | + */ |
| 164 | +extern ptk_thread_handle_t ptk_thread_get_parent(ptk_thread_handle_t thread); |
| 165 | + |
| 166 | +/** |
| 167 | + * @brief Count the number of child threads. |
123 | 168 | * |
124 | | - * @param data Pointer to user data passed to the thread. |
| 169 | + * Child threads are automatically tracked. This returns the current count. |
| 170 | + * |
| 171 | + * @param parent Parent thread handle |
| 172 | + * @return Number of active child threads |
125 | 173 | */ |
126 | | -typedef void (*ptk_thread_func)(void *data); |
| 174 | +extern int ptk_thread_count_children(ptk_thread_handle_t parent); |
127 | 175 |
|
128 | 176 | /** |
129 | | - * @brief Creates and starts a new thread. |
| 177 | + * @brief Signal all child threads. |
| 178 | + * |
| 179 | + * Sends the same signal to all children of the specified parent thread. |
| 180 | + * Useful for coordinated shutdown or other broadcast operations. |
130 | 181 | * |
131 | | - * @param func Entry point for the thread function. |
132 | | - * @param data User data to pass to the thread function. |
133 | | - * @return Pointer to the created thread handle, or NULL on failure |
| 182 | + * @param parent Parent thread handle |
| 183 | + * @param signal_type Signal to send to all children |
| 184 | + * @return Error code |
134 | 185 | */ |
135 | | -extern ptk_thread *ptk_thread_create(ptk_thread_func func, void *data); |
| 186 | +extern ptk_err ptk_thread_signal_all_children(ptk_thread_handle_t parent, ptk_thread_signal_t signal_type); |
136 | 187 |
|
137 | 188 | /** |
138 | | - * @brief Waits for the specified thread to complete. |
| 189 | + * @brief Clean up dead child threads. |
| 190 | + * |
| 191 | + * Attempts to release handles for child threads that have died. Uses timeout |
| 192 | + * to avoid blocking. Call with PTK_TIME_NO_WAIT for immediate cleanup only. |
139 | 193 | * |
140 | | - * @param thread Thread to join. |
141 | | - * @return Error code. |
| 194 | + * @param parent Parent thread handle |
| 195 | + * @param timeout_ms Maximum time to wait for cleanup |
| 196 | + * @return Error code |
142 | 197 | */ |
143 | | -extern ptk_err ptk_thread_join(ptk_thread *thread); |
| 198 | +extern ptk_err ptk_thread_cleanup_dead_children(ptk_thread_handle_t parent, ptk_time_ms timeout_ms); |
144 | 199 |
|
0 commit comments