Mstdlib-1.24.0
m_thread_system.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2015 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_THREAD_SYSTEM_H__
25#define __M_THREAD_SYSTEM_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31
32#ifdef _WIN32
33# include <Winsock2.h>
34# include <Windows.h>
35# if _WIN32_WINNT < 0x0600
36 struct pollfd {
37 SOCKET fd; /* file descriptor */
38 short events; /* events to look for */
39 short revents; /* events returned */
40 };
41# define POLLIN 0x0001 /* any readable data available */
42# define POLLOUT 0x0004 /* file descriptor is writeable */
43# define POLLERR 0x0008 /* some poll error occurred */
44# define POLLHUP 0x0010 /* file descriptor was "hung up" */
45# define POLLNVAL 0x0020 /* requested events "invalid" */
46# endif
47 typedef unsigned int nfds_t;
48#else
49/* Need to include struct pollfd for M_thread_poll() */
50# include <poll.h>
51/* Needed for M_thread_sigmask() */
52# include <signal.h>
53#endif
54
55
56/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
57
58__BEGIN_DECLS
59
60/*! \addtogroup m_thread_system Thread - System Specific
61 * \ingroup m_thread_common
62 *
63 * Low level threading functionality. These are only provided due to fundamental and
64 * irreconcilable differences that they cannot be provided in a platform agnostic manner.
65 *
66 * @{
67 */
68
69/*! Monitor a file descriptor waiting for it to become ready for I/O operations.
70 *
71 * \param[in] fds Array of FDs to monitor with monitoring flags
72 * \param[in] nfds Number of file descriptors in array.
73 * \param[in] timeout How long to wait before giving up in ms. -1 = infinite
74 *
75 * \return The number of ready fds, 0 if timeout, -1 on error.
76 */
77M_API int M_thread_poll(struct pollfd fds[], nfds_t nfds, int timeout);
78
79#ifndef _WIN32
80/*! Examine and change blocked signals.
81 *
82 * \param[in] how How to change signal behavior. Values are
83 * - SIG_BLOCK
84 * - SIG_UNBLOCK
85 * - SIG_SETMASK
86 * \param[in] set What signals to set using how. Optional, NULL if only getting current signals from oldset.
87 * \param[in] oldset Previous signal status. Optional, NULL if only setting signals.
88 *
89 * \return M_TRUE on success, otherwise M_FALSE.
90 */
91M_API M_bool M_thread_sigmask(int how, const sigset_t *set, sigset_t *oldset);
92#endif
93
94/*! @} */
95
96__END_DECLS
97
98#endif /* __M_THREAD_SYSTEM_H__ */
int M_thread_poll(struct pollfd fds[], nfds_t nfds, int timeout)
M_bool M_thread_sigmask(int how, const sigset_t *set, sigset_t *oldset)