Skip to content

Commit

Permalink
Tidy and improve code for session discovery
Browse files Browse the repository at this point in the history
1. Add brackets round if bodies.
2. Use common os_calls/thread_calls functions where available and add
   new ones for:
     g_file_seek_rel_end()
     g_map_file_shared()
     g_unmap_file_shared()
     g_sleep_secs() [because usleep() can error for argument >= 1M]
     g_ftruncate()
     tc_mutex_timed_lock()
     tc_shared_mutex_create()
3. Separate declarations and initialisations.
4. Make macros for sesshm_try_lock() etc cleaner.

Also some minor bugfixes:

1. Fix bug in sesshm_try_lock() which begins "return 0".
2. The return value of pthread_mutex_init() in sesshm_create_new_shm()
   was ignored.  (No longer relevant because tc_shared_mutex_create() is
   now used.)
3. sesshm_map() set g_shm_mapping and returned a value.  (No longer
   relevant because using g_map_file_shared() it is simple enough not to
   need a function.)
  • Loading branch information
ben-cohen committed Jul 17, 2017
1 parent 1b50ef7 commit 9c172bd
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 123 deletions.
77 changes: 77 additions & 0 deletions common/os_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include <pwd.h>
#include <time.h>
#include <grp.h>
#include <sys/mman.h>
#endif

#include <stdlib.h>
Expand Down Expand Up @@ -1337,6 +1338,17 @@ g_sleep(int msecs)
#endif
}

/*****************************************************************************/
void
g_sleep_secs(int secs)
{
#if defined(_WIN32)
Sleep(secs * 1000);
#else
sleep(secs);
#endif
}

/*****************************************************************************/
int
g_sck_last_error_would_block(int sck)
Expand Down Expand Up @@ -1968,6 +1980,17 @@ g_memcmp(const void *s1, const void *s2, int len)
return memcmp(s1, s2, len);
}

/*****************************************************************************/
int
g_ftruncate(int fd, int length)
{
#if defined(_WIN32)
return -1;
#else
return ftruncate(fd, length);
#endif
}

/*****************************************************************************/
/* returns -1 on error, else return handle or file descriptor */
int
Expand Down Expand Up @@ -2109,6 +2132,31 @@ g_file_seek(int fd, int offset)
#endif
}

/*****************************************************************************/
/* move file pointer relative to end of file,
* returns offset on success, -1 on failure */
int
g_file_seek_rel_end(int fd, int offset)
{
#if defined(_WIN32)
int rv;

rv = (int)SetFilePointer((HANDLE)fd, offset, 0, FILE_END);

if (rv == (int)INVALID_SET_FILE_POINTER)
{
return -1;
}
else
{
return rv;
}

#else
return (int)lseek(fd, offset, SEEK_END);
#endif
}

/*****************************************************************************/
/* do a write lock on a file */
/* return boolean */
Expand Down Expand Up @@ -2907,6 +2955,35 @@ g_get_proc_address(long lib, const char *name)
#endif
}

/*****************************************************************************/
/* does not work in win32 */
void *
g_map_file_shared(int fd, int length)
{
void *mapped;

mapped = mmap(NULL,
length,
PROT_READ|PROT_WRITE,
MAP_SHARED,
fd,
0);

/* MAP_FAILED != NULL but mmap() will not return 0 unless MAP_FIXED is
* specified. */
if (mapped == MAP_FAILED)
mapped = NULL;
return mapped;
}

/*****************************************************************************/
/* does not work in win32 */
int
g_unmap_file_shared(void *mapped, int length)
{
return munmap(mapped, length);
}

/*****************************************************************************/
/* does not work in win32 */
int
Expand Down
5 changes: 5 additions & 0 deletions common/os_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ int g_sck_can_recv(int sck, int millis);
int g_sck_select(int sck1, int sck2);
void g_write_ip_address(int rcv_sck, char* ip_address, int bytes);
void g_sleep(int msecs);
void g_sleep_secs(int secs);
tintptr g_create_wait_obj(const char *name);
tintptr g_create_wait_obj_from_socket(tintptr socket, int write);
void g_delete_wait_obj_from_socket(tintptr wait_obj);
Expand All @@ -95,13 +96,15 @@ int g_obj_wait(tintptr* read_objs, int rcount, tintptr* write_objs,
void g_random(char* data, int len);
int g_abs(int i);
int g_memcmp(const void* s1, const void* s2, int len);
int g_ftruncate(int fd, int length);
int g_file_open(const char* file_name);
int g_file_open_ex(const char *file_name, int aread, int awrite,
int acreate, int atrunc);
int g_file_close(int fd);
int g_file_read(int fd, char* ptr, int len);
int g_file_write(int fd, const char *ptr, int len);
int g_file_seek(int fd, int offset);
int g_file_seek_rel_end(int fd, int offset);
int g_file_lock(int fd, int start, int len);
int g_chmod_hex(const char* filename, int flags);
int g_chown(const char* name, int uid, int gid);
Expand Down Expand Up @@ -139,6 +142,8 @@ int g_strtrim(char* str, int trim_flags);
long g_load_library(char* in);
int g_free_library(long lib);
void* g_get_proc_address(long lib, const char* name);
void* g_map_file_shared(int fd, int length);
int g_unmap_file_shared(void *mapped, int length);
int g_system(char* aexec);
char* g_get_strerror(void);
int g_get_errno(void);
Expand Down
44 changes: 44 additions & 0 deletions common/thread_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,34 @@ tc_mutex_create(void)
#endif
}

/*****************************************************************************/
int
tc_shared_mutex_create(tbus mutex)
{
#if defined(_WIN32)
/* Not implemented yet */
return 1;
#else
pthread_mutexattr_t mutexattr;
int rc;

rc = pthread_mutexattr_init(&mutexattr);
if (rc != 0)
{
return 1;
}

rc = pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);
if (rc != 0)
{
return 1;
}

rc = pthread_mutex_init((pthread_mutex_t *)mutex, &mutexattr);
return rc;
#endif
}

/*****************************************************************************/
void
tc_mutex_delete(tbus mutex)
Expand Down Expand Up @@ -140,6 +168,22 @@ tc_mutex_lock(tbus mutex)
#endif
}

/*****************************************************************************/
int
tc_mutex_timed_lock(tbus mutex, int timeout_ms)
{
#if defined(_WIN32)
WaitForSingleObject((HANDLE)mutex, timeout_ms);
return 0;
#else
struct timespec timeout;
timeout.tv_sec = timeout_ms / 1000;
timeout.tv_nsec = ((long)timeout_ms % 1000) * 1000;

return pthread_mutex_timedlock((pthread_mutex_t *)mutex, &timeout);
#endif
}

/*****************************************************************************/
int
tc_mutex_unlock(tbus mutex)
Expand Down
4 changes: 4 additions & 0 deletions common/thread_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ int
tc_threadid_equal(tbus tid1, tbus tid2);
tbus
tc_mutex_create(void);
int
tc_shared_mutex_create(tbus mutex);
void
tc_mutex_delete(tbus mutex);
int
tc_mutex_lock(tbus mutex);
int
tc_mutex_timed_lock(tbus mutex, int timeout_ms);
int
tc_mutex_unlock(tbus mutex);
tbus
tc_sem_create(int init_count);
Expand Down
Loading

0 comments on commit 9c172bd

Please sign in to comment.