1// Utils.h
2
3#ifndef UTILS_H
4#define UTILS_H
5
6#ifdef HAIKU_TARGET_PLATFORM_BEOS
7#	include <socket.h>
8#else
9#	include <sys/socket.h>
10#	include <unistd.h>
11#endif
12
13#include <SupportDefs.h>
14
15#include "Compatibility.h"
16
17template<typename T> T max(const T& a, const T& b) { return (a > b ? a : b); }
18template<typename T> T min(const T& a, const T& b) { return (a < b ? a : b); }
19
20// safe_closesocket
21/*!	There seems to be race condition on a net_server system, if two threads
22	try to close the same socket at the same time. This is work-around. The
23	variable which stores the socket ID must be a vint32.
24*/
25static inline
26void
27safe_closesocket(int32& socketVar)
28{
29	int32 socket = atomic_or(&socketVar, -1);
30#ifdef __HAIKU__
31	close(socket);
32#else
33	if (socket >= 0) {
34#	ifndef HAIKU_TARGET_PLATFORM_BEOS
35		shutdown(socket, SHUTDOWN_BOTH);
36#	endif
37		closesocket(socket);
38	}
39#endif
40}
41
42#endif	// UTILS_H
43