1/* ath.h - Thread-safeness library.
2   Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3
4   This file is part of Libgcrypt.
5
6   Libgcrypt is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of
9   the License, or (at your option) any later version.
10
11   Libgcrypt is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with Libgcrypt; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.  */
20
21#ifndef ATH_H
22#define ATH_H
23
24#include <config.h>
25
26#ifdef _WIN32
27# include <windows.h>
28#else /* !_WIN32 */
29# ifdef HAVE_SYS_SELECT_H
30#  include <sys/select.h>
31# else
32#  include <sys/time.h>
33# endif
34# include <sys/types.h>
35# ifdef HAVE_SYS_MSG_H
36#  include <sys/msg.h>  /* (e.g. for zOS) */
37# endif
38# include <sys/socket.h>
39#endif /* !_WIN32 */
40#include <gpg-error.h>
41
42
43
44/* Define _ATH_EXT_SYM_PREFIX if you want to give all external symbols
45   a prefix.  */
46#define _ATH_EXT_SYM_PREFIX _gcry_
47
48#ifdef _ATH_EXT_SYM_PREFIX
49#define _ATH_PREFIX1(x,y) x ## y
50#define _ATH_PREFIX2(x,y) _ATH_PREFIX1(x,y)
51#define _ATH_PREFIX(x) _ATH_PREFIX2(_ATH_EXT_SYM_PREFIX,x)
52#define ath_install _ATH_PREFIX(ath_install)
53#define ath_init _ATH_PREFIX(ath_init)
54#define ath_mutex_init _ATH_PREFIX(ath_mutex_init)
55#define ath_mutex_destroy _ATH_PREFIX(ath_mutex_destroy)
56#define ath_mutex_lock _ATH_PREFIX(ath_mutex_lock)
57#define ath_mutex_unlock _ATH_PREFIX(ath_mutex_unlock)
58#define ath_read _ATH_PREFIX(ath_read)
59#define ath_write _ATH_PREFIX(ath_write)
60#define ath_select _ATH_PREFIX(ath_select)
61#define ath_waitpid _ATH_PREFIX(ath_waitpid)
62#define ath_connect _ATH_PREFIX(ath_connect)
63#define ath_accept _ATH_PREFIX(ath_accept)
64#define ath_sendmsg _ATH_PREFIX(ath_sendmsg)
65#define ath_recvmsg _ATH_PREFIX(ath_recvmsg)
66#endif
67
68
69enum ath_thread_option
70  {
71    ATH_THREAD_OPTION_DEFAULT = 0,
72    ATH_THREAD_OPTION_USER = 1,
73    ATH_THREAD_OPTION_PTH = 2,
74    ATH_THREAD_OPTION_PTHREAD = 3
75  };
76
77struct ath_ops
78{
79  /* The OPTION field encodes the thread model and the version number
80     of this structure.
81       Bits  7 - 0  are used for the thread model
82       Bits 15 - 8  are used for the version number.
83  */
84  unsigned int option;
85
86  int (*init) (void);
87  int (*mutex_init) (void **priv);
88  int (*mutex_destroy) (void *priv);
89  int (*mutex_lock) (void *priv);
90  int (*mutex_unlock) (void *priv);
91  ssize_t (*read) (int fd, void *buf, size_t nbytes);
92  ssize_t (*write) (int fd, const void *buf, size_t nbytes);
93#ifdef _WIN32
94  ssize_t (*select) (int nfd, void *rset, void *wset, void *eset,
95		     struct timeval *timeout);
96  ssize_t (*waitpid) (pid_t pid, int *status, int options);
97  int (*accept) (int s, void  *addr, int *length_ptr);
98  int (*connect) (int s, void *addr, int length);
99  int (*sendmsg) (int s, const void *msg, int flags);
100  int (*recvmsg) (int s, void *msg, int flags);
101#else
102  ssize_t (*select) (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
103		     struct timeval *timeout);
104  ssize_t (*waitpid) (pid_t pid, int *status, int options);
105  int (*accept) (int s, struct sockaddr *addr, socklen_t *length_ptr);
106  int (*connect) (int s, struct sockaddr *addr, socklen_t length);
107  int (*sendmsg) (int s, const struct msghdr *msg, int flags);
108  int (*recvmsg) (int s, struct msghdr *msg, int flags);
109#endif
110};
111
112gpg_err_code_t ath_install (struct ath_ops *ath_ops, int check_only);
113int ath_init (void);
114
115
116/* Functions for mutual exclusion.  */
117typedef void *ath_mutex_t;
118#define ATH_MUTEX_INITIALIZER 0
119
120int ath_mutex_init (ath_mutex_t *mutex);
121int ath_mutex_destroy (ath_mutex_t *mutex);
122int ath_mutex_lock (ath_mutex_t *mutex);
123int ath_mutex_unlock (ath_mutex_t *mutex);
124
125/* Replacement for the POSIX functions, which can be used to allow
126   other (user-level) threads to run.  */
127ssize_t ath_read (int fd, void *buf, size_t nbytes);
128ssize_t ath_write (int fd, const void *buf, size_t nbytes);
129#ifdef _WIN32
130ssize_t ath_select (int nfd, void *rset, void *wset, void *eset,
131		    struct timeval *timeout);
132ssize_t ath_waitpid (pid_t pid, int *status, int options);
133int ath_accept (int s, void *addr, int *length_ptr);
134int ath_connect (int s, void *addr, int length);
135int ath_sendmsg (int s, const void *msg, int flags);
136int ath_recvmsg (int s, void *msg, int flags);
137#else
138ssize_t ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset,
139		    struct timeval *timeout);
140ssize_t ath_waitpid (pid_t pid, int *status, int options);
141int ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr);
142int ath_connect (int s, struct sockaddr *addr, socklen_t length);
143int ath_sendmsg (int s, const struct msghdr *msg, int flags);
144int ath_recvmsg (int s, struct msghdr *msg, int flags);
145#endif
146
147#endif	/* ATH_H */
148