Ruby  3.4.0dev (2024-12-06 revision 892c46283a5ea4179500d951c9d4866c0051f27b)
Context.h
1 /*
2  * This file is part of the "Coroutine" project and released under the MIT License.
3  *
4  * Created by Samuel Williams on 24/6/2021.
5  * Copyright, 2021, by Samuel Williams.
6 */
7 
8 #pragma once
9 
10 #include <assert.h>
11 #include <stddef.h>
12 #include <pthread.h>
13 
14 #define COROUTINE void
15 
16 #define COROUTINE_PTHREAD_CONTEXT
17 
18 #ifdef HAVE_STDINT_H
19 #include <stdint.h>
20 #if INTPTR_MAX <= INT32_MAX
21 #define COROUTINE_LIMITED_ADDRESS_SPACE
22 #endif
23 #endif
24 
25 struct coroutine_context;
26 
28 {
29  pthread_mutex_t guard;
30  struct coroutine_context * main;
31 
32  size_t count;
33 };
34 
35 typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
36 
37 struct coroutine_context
38 {
39  struct coroutine_shared * shared;
40 
41  coroutine_start start;
42  void *argument;
43 
44  void *stack;
45  size_t size;
46 
47  pthread_t id;
48  pthread_cond_t schedule;
49  struct coroutine_context * from;
50 };
51 
52 void coroutine_initialize_main(struct coroutine_context * context);
53 
54 void coroutine_initialize(
55  struct coroutine_context *context,
56  coroutine_start start,
57  void *stack,
58  size_t size
59 );
60 
61 struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
62 
63 void coroutine_destroy(struct coroutine_context * context);