syslog-ng source
tls-support.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2011 Balabit
3  * Copyright (c) 1998-2011 Balázs Scheidler
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * As an additional exemption you are allowed to compile & link against the
20  * OpenSSL libraries as published by the OpenSSL project. See the file
21  * COPYING for details.
22  */
23 
24 #ifndef TLS_SUPPORT_H_INCLUDED
25 #define TLS_SUPPORT_H_INCLUDED
26 
27 /* this header is about Thread Local Storage and not about Transport Layer Security */
28 
29 #include <syslog-ng-config.h>
30 
31 #if ! SYSLOG_NG_HAVE_THREAD_KEYWORD
32 
33 #include <stdlib.h>
34 #include <pthread.h>
35 
36 static struct __slng_tls_variables *
37 __slng_tls_init_thread(pthread_key_t key, size_t size)
38 {
39  struct __slng_tls_variables *ptr;
40 
41  ptr = calloc(1, size);
42  if (!ptr)
43  abort();
44 
45  pthread_setspecific(key, ptr);
46  return ptr;
47 }
48 
49 static inline struct __slng_tls_variables *__slng_tls_deref_helper(pthread_key_t key, size_t size)
50 {
51  struct __slng_tls_variables *ptr;
52 
53  ptr = pthread_getspecific(key);
54  if (!ptr)
55  ptr = __slng_tls_init_thread(key, size);
56  return ptr;
57 }
58 
59 #define TLS_BLOCK_START \
60  static pthread_key_t __slng_tls_key; \
61  static void __attribute__((constructor)) __slng_tls_init_key(void) \
62  { \
63  pthread_key_create(&__slng_tls_key, free); \
64  } \
65  \
66  struct __slng_tls_variables
67 
68 #define TLS_BLOCK_END
69 
70 #define __slng_tls_deref(var) (*({ struct __slng_tls_variables *__ptr = __slng_tls_deref_helper(__slng_tls_key, sizeof(struct __slng_tls_variables)); &__ptr->var; }))
71 
72 #define __thread #
73 
74 #else /* SYSLOG_NG_HAVE_TLS */
75 
76 #define TLS_BLOCK_START \
77  struct __slng_tls_variables
78 
79 #define TLS_BLOCK_END \
80  ; \
81  static __thread struct __slng_tls_variables __slng_tls
82 
83 
84 #define __slng_tls_deref(var) (__slng_tls.var)
85 
86 #endif
87 
88 #endif
struct tm key
Definition: cache.c:63