Ruby 3.5.0dev (2025-02-23 revision fd882fb6819fb8b48b09e24ff71748d1bae35e43)
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
26
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
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);