Define a value for SYS_F_FCNTL
[openssl.git] / crypto / threads_none.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/crypto.h>
11 #include <internal/cryptlib.h>
12
13 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
14
15 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
16 {
17     CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(unsigned int));
18     if (lock == NULL)
19         return NULL;
20
21     *(unsigned int *)lock = 1;
22
23     return lock;
24 }
25
26 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
27 {
28     OPENSSL_assert(*(unsigned int *)lock == 1);
29     return 1;
30 }
31
32 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
33 {
34     OPENSSL_assert(*(unsigned int *)lock == 1);
35     return 1;
36 }
37
38 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
39 {
40     OPENSSL_assert(*(unsigned int *)lock == 1);
41     return 1;
42 }
43
44 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
45     if (lock == NULL)
46         return;
47
48     *(unsigned int *)lock = 0;
49     OPENSSL_free(lock);
50
51     return;
52 }
53
54 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
55 {
56     if (*once != 0)
57         return 1;
58
59     init();
60     *once = 1;
61
62     return 1;
63 }
64
65 #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
66
67 static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
68
69 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
70 {
71     static unsigned int thread_local_key = 0;
72
73     if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
74         return 0;
75
76     *key = thread_local_key++;
77
78     thread_local_storage[*key] = NULL;
79
80     return 1;
81 }
82
83 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
84 {
85     if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
86         return NULL;
87
88     return thread_local_storage[*key];
89 }
90
91 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
92 {
93     if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
94         return 0;
95
96     thread_local_storage[*key] = val;
97
98     return 1;
99 }
100
101 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
102 {
103     *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
104     return 1;
105 }
106
107 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
108 {
109     return 0;
110 }
111
112 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
113 {
114     return (a == b);
115 }
116
117 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
118 {
119     *val += amount;
120     *ret  = *val;
121
122     return 1;
123 }
124
125 int openssl_init_fork_handlers(void)
126 {
127     return 0;
128 }
129
130 #endif