Skip to content

Commit

Permalink
[libposix] update timer/print/thread api
Browse files Browse the repository at this point in the history
  • Loading branch information
gozfree committed Sep 19, 2021
1 parent c07f9e7 commit 20b7250
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 29 deletions.
13 changes: 11 additions & 2 deletions gear-lib/libposix/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ LIBNAME = libposix
VER_TAG = $(shell echo ${LIBNAME} | tr 'a-z' 'A-Z')
VER = $(shell awk '/'"${VER_TAG}_VERSION"'/{print $$3}' ${LIBNAME}.h)
TGT_LIB_H = $(LIBNAME).h
TGT_LIB_H += kernel_list.h gear_misc.h
TGT_LIB_H += kernel_list.h libposix_ext.h
ifeq ($(ARCH), win)
TGT_LIB_H += libposix4win.h kernel_list_win32.h
endif
TGT_LIB_A = $(LIBNAME).a
TGT_LIB_SO = $(LIBNAME).so
TGT_LIB_SO_VER = $(TGT_LIB_SO).${VER}
TGT_UNIT_TEST = test_$(LIBNAME)

OBJS_LIB = $(LIBNAME).o libposix4linux.o
OBJS_LIB = $(LIBNAME).o
ifeq ($(ARCH), linux)
OBJS_LIB += libposix4linux.o
endif
ifeq ($(ARCH), win)
OBJS_LIB += libposix4win.o
endif
OBJS_UNIT_TEST = test_$(LIBNAME).o

###############################################################################
Expand Down
2 changes: 1 addition & 1 deletion gear-lib/libposix/libposix.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ extern "C" {
#error "OS_UNDEFINED"
#endif

#include "gear_misc.h"
#include "libposix_ext.h"

#ifdef __cplusplus
}
Expand Down
48 changes: 40 additions & 8 deletions gear-lib/libposix/libposix4win.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int access(const char *file, int mode)
return ret;
}

static unsigned __stdcall __thread_func(void *arg)
static long unsigned int __stdcall __thread_func(void *arg)
{
pthread_win_t *h = (pthread_win_t *)arg;
h->ret = h->func(h->arg);
Expand Down Expand Up @@ -156,8 +156,12 @@ int pthread_mutex_unlock(pthread_mutex_t *m)

int pthread_rwlock_init(pthread_rwlock_t *m, void* attr)
{
#if _WIN32_WINNT >= 0x0600
InitializeSRWLock(m);
return 0;
#else
return -1;
#endif
}

int pthread_rwlock_destroy(pthread_rwlock_t *m)
Expand All @@ -168,14 +172,22 @@ int pthread_rwlock_destroy(pthread_rwlock_t *m)

int pthread_rwlock_rdlock(pthread_rwlock_t *m)
{
#if _WIN32_WINNT >= 0x0600
AcquireSRWLockExclusive(m);
return 0;
#else
return -1;
#endif
}

int pthread_rwlock_unlock(pthread_rwlock_t *m)
{
#if _WIN32_WINNT >= 0x0600
ReleaseSRWLockExclusive(m);
return 0;
#else
return -1;
#endif
}

int sem_init(sem_t *sem, int pshared, unsigned int value)
Expand Down Expand Up @@ -205,18 +217,26 @@ int sem_post(sem_t *sem)

int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
{
#if _WIN32_WINNT >= 0x0600
BOOL pending = FALSE;
InitOnceBeginInitialize(once_control, 0, &pending, NULL);
if (pending)
init_routine();
InitOnceComplete(once_control, 0, NULL);
return 0;
#else
return -1;
#endif
}

int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
{
#if _WIN32_WINNT >= 0x0600
InitializeConditionVariable(cond);
return 0;
#else
return -1;
#endif
}

int pthread_cond_destroy(pthread_cond_t *cond)
Expand All @@ -226,30 +246,44 @@ int pthread_cond_destroy(pthread_cond_t *cond)

int pthread_cond_broadcast(pthread_cond_t *cond)
{
#if _WIN32_WINNT >= 0x0600
WakeAllConditionVariable(cond);
return 0;
#else
return -1;
#endif
}

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
#if _WIN32_WINNT >= 0x0600
SleepConditionVariableSRW(cond, (PSRWLOCK)mutex, INFINITE, 0);
return 0;
#else
return -1;
#endif
}

int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, int ms)
{
#if _WIN32_WINNT >= 0x0600
SleepConditionVariableSRW(cond, (PSRWLOCK)mutex, ms, 0);
return 0;
#else
return -1;
#endif
}


int pthread_cond_signal(pthread_cond_t *cond)
{
#if _WIN32_WINNT >= 0x0600
WakeConditionVariable(cond);
return 0;
#else
return -1;
#endif
}


int utf8towchar(const char *filename_utf8, wchar_t **filename_w)
{
int num_chars;
Expand Down Expand Up @@ -328,7 +362,7 @@ void showProcessInformation()
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe32)) {
do {
printf("pid %d %s\n", pe32.th32ProcessID, pe32.szExeFile);
printf("pid %ld %s\n", pe32.th32ProcessID, pe32.szExeFile);
} while(Process32Next(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
Expand All @@ -339,7 +373,6 @@ int get_proc_name(char *name, size_t len)
{
PROCESSENTRY32 pe32;
int got = 0;
int i = 0;
HANDLE hd = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (!hd) {
return -1;
Expand Down Expand Up @@ -408,7 +441,6 @@ int gettimeofday(struct timeval *tv, struct timezone *tz)
return 0;
}


void usleep(unsigned long usec)
{
HANDLE timer;
Expand Down Expand Up @@ -477,10 +509,10 @@ int get_nprocs()

void *align_malloc(size_t size, size_t align)
{
long diff;
size_t diff;
void *ptr = malloc(size + align);
if (ptr) {
diff = ((~(long)ptr) & (align - 1)) + 1;
diff = ((~(size_t)ptr) & (align - 1)) + 1;
ptr = (char *)ptr + diff;
((char *)ptr)[-1] = (char)diff;
}
Expand Down
55 changes: 46 additions & 9 deletions gear-lib/libposix/libposix4win.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ typedef int bool;
#define __func__ __FUNCTION__

typedef SSIZE_T ssize_t;
#ifndef _FILE_OFFSET_BITS_SET_OFFT
typedef SSIZE_T off_t;
#endif


/******************************************************************************
Expand All @@ -66,14 +68,14 @@ typedef SSIZE_T off_t;


#define PATH_SPLIT '\\'
#define PRId8 "hhd"
#define PRId16 "hd"
#define PRId32 "ld"
#define PRId64 "lld"
#define PRIu8 "hhu"
#define PRIu16 "hu"
#define PRIu32 "lu"
#define PRIu64 "llu"
#define PRId8 "d"
#define PRId16 "d"
#define PRId32 "d"
#define PRId64 "I64d"
#define PRIu8 "u"
#define PRIu16 "u"
#define PRIu32 "u"
#define PRIu64 "I64u"

#define iovec _WSABUF
#define iov_len len
Expand All @@ -90,9 +92,13 @@ GEAR_API char *dup_wchar_to_utf8(wchar_t *w);
#define STDOUT_FILENO 1 /* standard output file descriptor */
#define STDERR_FILENO 2 /* standard error file descriptor */
#define MAXPATHLEN 1024
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif

#ifndef _MODE_T_
typedef int mode_t;
#endif

#define F_OK 0
#define R_OK 4
Expand Down Expand Up @@ -184,22 +190,53 @@ struct win_time_t
int msec;/** This represents the milisecond part, with the value is 0-999 */
};

#ifndef _TIMEZONE_DEFINED
struct timezone
{
int tz_minuteswest;
int tz_dsttime;
};
#endif

GEAR_API int gettimeofday(struct timeval *tv, struct timezone *tz);
#define localtime_r(timep,result) localtime_s(result, timep)

#define timeradd(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
if ((vvp)->tv_usec >= 1000000) { \
(vvp)->tv_sec++; \
(vvp)->tv_usec -= 1000000; \
} \
} while (0)

#define timersub(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
if ((vvp)->tv_usec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_usec += 1000000; \
} \
} while (0)

#define localtime_r(timep,result) ERR_PTR(localtime_s(result, timep)==0?true:false)
#define sleep(n) Sleep(n*1000)
GEAR_API void usleep(unsigned long usec);

typedef int clockid_t;
#ifndef CLOCK_REALTIME
#define CLOCK_REALTIME ((clockid_t)1)
#endif
#ifndef CLOCK_PROCESS_CPUTIME_ID
#define CLOCK_PROCESS_CPUTIME_ID ((clockid_t)2)
#endif
#ifndef CLOCK_THREAD_CPUTIME_ID
#define CLOCK_THREAD_CPUTIME_ID ((clockid_t)3)
#endif
#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC ((clockid_t)4)
#endif


/******************************************************************************
Expand Down
67 changes: 58 additions & 9 deletions gear-lib/libposix/gear_misc.h → gear-lib/libposix/libposix_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,63 @@
extern "C" {
#endif

/******************************************************************************
* BASIC MACRO DEFINES
******************************************************************************/

#ifdef __GNUC__
#define LIKELY(x) (__builtin_expect(!!(x), 1))
#define UNLIKELY(x) (__builtin_expect(!!(x), 0))
#else
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif


#define MAX_ERRNO (4095)
#define IS_ERR_VALUE(x) UNLIKELY((unsigned long)(intptr_t)(void *)(intptr_t)(x) >= (unsigned long)-MAX_ERRNO)

static inline void *ERR_PTR(long error)
{
return (void *) (intptr_t)error;
}

static inline long PTR_ERR(const void *ptr)
{
return (long) (intptr_t)ptr;
}

static inline bool IS_ERR(const void *ptr)
{
return IS_ERR_VALUE((unsigned long)(intptr_t)ptr);
}

static inline bool IS_ERR_OR_NULL(const void *ptr)
{
return UNLIKELY(!ptr) || IS_ERR_VALUE((unsigned long)(intptr_t)ptr);
}

/**
* ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
* @ptr: The pointer to cast.
*
* Explicitly cast an error-valued pointer to another pointer type in such a
* way as to make it clear that's what's going on.
*/
static inline void * ERR_CAST(const void *ptr)
{
/* cast away the const */
return (void *) ptr;
}

static inline int PTR_ERR_OR_ZERO(const void *ptr)
{
if (IS_ERR(ptr))
return PTR_ERR(ptr);
else
return 0;
}

/******************************************************************************
* MACRO DEFINES ARE UPPERCASE
******************************************************************************/
Expand All @@ -39,14 +96,6 @@ typedef struct rational {
*/
#define UNUSED(e, ...) (void) ((void) (e), ##__VA_ARGS__)

#ifdef __GNUC__
#define LIKELY(x) (__builtin_expect(!!(x), 1))
#define UNLIKELY(x) (__builtin_expect(!!(x), 0))
#else
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif

#define SWAP(a, b) \
do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)

Expand Down Expand Up @@ -83,7 +132,7 @@ typedef struct rational {
printf("%s\n", _tmp); \
} while (0)

#define ALIGN2(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define ALIGN2(x, a) (((x) + (a) - 1) & ~((a) - 1))

#define is_alpha(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))

Expand Down

0 comments on commit 20b7250

Please sign in to comment.