22
33#include "run_scheduler.h"
44
5- #include <pthread.h>
65#include <stdio.h>
76#include <stdlib.h>
87#include <string.h>
1211 * ======================================================================== */
1312
1413struct run_chan {
15- pthread_mutex_t lock ;
14+ run_mutex_t lock ;
1615
1716 size_t elem_size ; /* size of each element in bytes */
1817 size_t buffer_cap ; /* buffer capacity (0 = unbuffered) */
@@ -38,7 +37,7 @@ run_chan_t *run_chan_new(size_t elem_size, size_t buffer_cap) {
3837 abort ();
3938 }
4039
41- pthread_mutex_init (& ch -> lock , NULL );
40+ run_mutex_init (& ch -> lock );
4241 ch -> elem_size = elem_size ;
4342 ch -> buffer_cap = buffer_cap ;
4443 ch -> buffer_len = 0 ;
@@ -63,7 +62,7 @@ run_chan_t *run_chan_new(size_t elem_size, size_t buffer_cap) {
6362void run_chan_free (run_chan_t * ch ) {
6463 if (!ch )
6564 return ;
66- pthread_mutex_destroy (& ch -> lock );
65+ run_mutex_destroy (& ch -> lock );
6766 free (ch -> buffer );
6867 free (ch );
6968}
@@ -79,10 +78,10 @@ void run_chan_free(run_chan_t *ch) {
7978 * ======================================================================== */
8079
8180void run_chan_send (run_chan_t * ch , const void * data ) {
82- pthread_mutex_lock (& ch -> lock );
81+ run_mutex_lock (& ch -> lock );
8382
8483 if (ch -> closed ) {
85- pthread_mutex_unlock (& ch -> lock );
84+ run_mutex_unlock (& ch -> lock );
8685 fprintf (stderr , "run: send on closed channel\n" );
8786 abort ();
8887 }
@@ -92,7 +91,7 @@ void run_chan_send(run_chan_t *ch, const void *data) {
9291 run_g_t * receiver = run_g_queue_pop (& ch -> recv_q );
9392 /* Direct copy: data -> receiver's waiting slot */
9493 memcpy (receiver -> chan_data_ptr , data , ch -> elem_size );
95- pthread_mutex_unlock (& ch -> lock );
94+ run_mutex_unlock (& ch -> lock );
9695 run_g_ready (receiver );
9796 return ;
9897 }
@@ -103,7 +102,7 @@ void run_chan_send(run_chan_t *ch, const void *data) {
103102 memcpy (slot , data , ch -> elem_size );
104103 ch -> send_idx = (ch -> send_idx + 1 ) % ch -> buffer_cap ;
105104 ch -> buffer_len ++ ;
106- pthread_mutex_unlock (& ch -> lock );
105+ run_mutex_unlock (& ch -> lock );
107106 return ;
108107 }
109108
@@ -112,7 +111,7 @@ void run_chan_send(run_chan_t *ch, const void *data) {
112111 if (!g ) {
113112 /* Called from main thread before scheduler is running --
114113 * this would deadlock. */
115- pthread_mutex_unlock (& ch -> lock );
114+ run_mutex_unlock (& ch -> lock );
116115 fprintf (stderr , "run: channel send would block on main thread\n" );
117116 abort ();
118117 }
@@ -121,7 +120,7 @@ void run_chan_send(run_chan_t *ch, const void *data) {
121120 g -> chan_data_ptr = (void * )data ; /* sender's data stays in place */
122121 g -> chan_panic = false;
123122 run_g_queue_push (& ch -> send_q , g );
124- pthread_mutex_unlock (& ch -> lock );
123+ run_mutex_unlock (& ch -> lock );
125124
126125 run_schedule (); /* context switch to scheduler */
127126 /* Resumed here after a receiver copies our data */
@@ -138,7 +137,7 @@ void run_chan_send(run_chan_t *ch, const void *data) {
138137 * ======================================================================== */
139138
140139void run_chan_recv (run_chan_t * ch , void * data ) {
141- pthread_mutex_lock (& ch -> lock );
140+ run_mutex_lock (& ch -> lock );
142141
143142 /* Fast path: waiting sender exists */
144143 if (ch -> send_q .len > 0 ) {
@@ -158,7 +157,7 @@ void run_chan_recv(run_chan_t *ch, void *data) {
158157 /* Unbuffered: direct copy from sender */
159158 memcpy (data , sender -> chan_data_ptr , ch -> elem_size );
160159 }
161- pthread_mutex_unlock (& ch -> lock );
160+ run_mutex_unlock (& ch -> lock );
162161 run_g_ready (sender );
163162 return ;
164163 }
@@ -169,21 +168,21 @@ void run_chan_recv(run_chan_t *ch, void *data) {
169168 memcpy (data , slot , ch -> elem_size );
170169 ch -> recv_idx = (ch -> recv_idx + 1 ) % ch -> buffer_cap ;
171170 ch -> buffer_len -- ;
172- pthread_mutex_unlock (& ch -> lock );
171+ run_mutex_unlock (& ch -> lock );
173172 return ;
174173 }
175174
176175 /* Channel is closed and empty */
177176 if (ch -> closed ) {
178177 memset (data , 0 , ch -> elem_size ); /* zero value */
179- pthread_mutex_unlock (& ch -> lock );
178+ run_mutex_unlock (& ch -> lock );
180179 return ;
181180 }
182181
183182 /* Must block: buffer empty (or unbuffered with no sender) */
184183 run_g_t * g = run_current_g ();
185184 if (!g ) {
186- pthread_mutex_unlock (& ch -> lock );
185+ run_mutex_unlock (& ch -> lock );
187186 fprintf (stderr , "run: channel recv would block on main thread\n" );
188187 abort ();
189188 }
@@ -192,7 +191,7 @@ void run_chan_recv(run_chan_t *ch, void *data) {
192191 g -> chan_data_ptr = data ; /* receiver provides the destination */
193192 g -> chan_panic = false;
194193 run_g_queue_push (& ch -> recv_q , g );
195- pthread_mutex_unlock (& ch -> lock );
194+ run_mutex_unlock (& ch -> lock );
196195
197196 run_schedule (); /* context switch to scheduler */
198197 /* Resumed here after a sender copies data to our slot */
@@ -203,10 +202,10 @@ void run_chan_recv(run_chan_t *ch, void *data) {
203202 * ======================================================================== */
204203
205204void run_chan_close (run_chan_t * ch ) {
206- pthread_mutex_lock (& ch -> lock );
205+ run_mutex_lock (& ch -> lock );
207206
208207 if (ch -> closed ) {
209- pthread_mutex_unlock (& ch -> lock );
208+ run_mutex_unlock (& ch -> lock );
210209 fprintf (stderr , "run: close of closed channel\n" );
211210 abort ();
212211 }
@@ -232,7 +231,7 @@ void run_chan_close(run_chan_t *ch) {
232231 run_g_queue_push (& wake_list , g );
233232 }
234233
235- pthread_mutex_unlock (& ch -> lock );
234+ run_mutex_unlock (& ch -> lock );
236235
237236 /* Make all collected Gs runnable (outside the channel lock) */
238237 run_g_t * g ;
0 commit comments