- HAL: some changes to linux and BSD hal implementations

pull/147/head
Michael Zillgith 7 years ago
parent 0e7fcb0c29
commit 47f540a4f9

@ -428,8 +428,12 @@ Socket_write(Socket self, uint8_t* buf, int size)
if (self->fd == -1) if (self->fd == -1)
return -1; return -1;
// MSG_NOSIGNAL - prevent send to signal SIGPIPE when peer unexpectedly closed the socket int retVal = send(self->fd, buf, size, 0);
return send(self->fd, buf, size, 0);
if ((retVal == -1) && (errno == EAGAIN))
return 0;
else
return retVal;
} }
void void

@ -475,8 +475,13 @@ Socket_write(Socket self, uint8_t* buf, int size)
if (self->fd == -1) if (self->fd == -1)
return -1; return -1;
// MSG_NOSIGNAL - prevent send to signal SIGPIPE when peer unexpectedly closed the socket /* MSG_NOSIGNAL - prevent send to signal SIGPIPE when peer unexpectedly closed the socket */
return send(self->fd, buf, size, MSG_NOSIGNAL); int retVal = send(self->fd, buf, size, MSG_NOSIGNAL);
if ((retVal == -1) && (errno == EAGAIN))
return 0;
else
return retVal;
} }
void void

@ -25,23 +25,24 @@
#include <semaphore.h> #include <semaphore.h>
#include <unistd.h> #include <unistd.h>
#include "hal_thread.h" #include "hal_thread.h"
#include "libiec61850_platform_includes.h"
#include "lib_memory.h" #include "lib_memory.h"
struct sThread { struct sThread
ThreadExecutionFunction function; {
void* parameter; ThreadExecutionFunction function;
pthread_t pthread; void* parameter;
int state; pthread_t pthread;
bool autodestroy; int state;
bool autodestroy;
}; };
Semaphore Semaphore
Semaphore_create(int initialValue) Semaphore_create(int initialValue)
{ {
char tmpname[] = {"/tmp/libiec61850.XXXXXX"}; Semaphore self = GLOBAL_MALLOC(sizeof(sem_t));
mktemp(tmpname);
Semaphore self = sem_open(tmpname, O_CREAT, 0666, initialValue); sem_init((sem_t*) self, 0, initialValue);
return self; return self;
} }
@ -68,16 +69,16 @@ Semaphore_destroy(Semaphore self)
Thread Thread
Thread_create(ThreadExecutionFunction function, void* parameter, bool autodestroy) Thread_create(ThreadExecutionFunction function, void* parameter, bool autodestroy)
{ {
Thread thread = (Thread) GLOBAL_MALLOC(sizeof(struct sThread)); Thread thread = (Thread) GLOBAL_MALLOC(sizeof(struct sThread));
if (thread != NULL) { if (thread != NULL) {
thread->parameter = parameter; thread->parameter = parameter;
thread->function = function; thread->function = function;
thread->state = 0; thread->state = 0;
thread->autodestroy = autodestroy; thread->autodestroy = autodestroy;
} }
return thread; return thread;
} }
static void* static void*
@ -85,38 +86,38 @@ destroyAutomaticThread(void* parameter)
{ {
Thread thread = (Thread) parameter; Thread thread = (Thread) parameter;
thread->function(thread->parameter); thread->function(thread->parameter);
GLOBAL_FREEMEM(thread); GLOBAL_FREEMEM(thread);
pthread_exit(NULL); pthread_exit(NULL);
} }
void void
Thread_start(Thread thread) Thread_start(Thread thread)
{ {
if (thread->autodestroy == true) { if (thread->autodestroy == true) {
pthread_create(&thread->pthread, NULL, destroyAutomaticThread, thread); pthread_create(&thread->pthread, NULL, destroyAutomaticThread, thread);
pthread_detach(thread->pthread); pthread_detach(thread->pthread);
} }
else else
pthread_create(&thread->pthread, NULL, thread->function, thread->parameter); pthread_create(&thread->pthread, NULL, thread->function, thread->parameter);
thread->state = 1; thread->state = 1;
} }
void void
Thread_destroy(Thread thread) Thread_destroy(Thread thread)
{ {
if (thread->state == 1) { if (thread->state == 1) {
pthread_join(thread->pthread, NULL); pthread_join(thread->pthread, NULL);
} }
GLOBAL_FREEMEM(thread); GLOBAL_FREEMEM(thread);
} }
void void
Thread_sleep(int millies) Thread_sleep(int millies)
{ {
usleep(millies * 1000); usleep(millies * 1000);
} }

Loading…
Cancel
Save