Ruby: coroutine/pthread/Context.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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
25struct coroutine_context;
26
27 struct coroutine_shared
28{
29 pthread_mutex_t guard;
30 struct coroutine_context * main;
31
32 size_t count;
33};
34
35typedef COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
36
37struct 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
52void coroutine_initialize_main(struct coroutine_context * context);
53
54void coroutine_initialize(
55 struct coroutine_context *context,
56 coroutine_start start,
57 void *stack,
58 size_t size
59);
60
61struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
62
63void coroutine_destroy(struct coroutine_context * context);
assert.h
coroutine_context
Definition Context.h:36
coroutine_shared
Definition Context.h:28