diff --git a/arch/linux/common/tos_cpu.c b/arch/linux/common/tos_cpu.c index d1b16e22..aa600949 100644 --- a/arch/linux/common/tos_cpu.c +++ b/arch/linux/common/tos_cpu.c @@ -78,6 +78,7 @@ __API__ void tos_cpu_cpsr_restore(cpu_cpsr_t cpsr) __KERNEL__ void cpu_init(void) { + port_init(); k_cpu_cycle_per_tick = TOS_CFG_CPU_CLOCK / k_cpu_tick_per_second; cpu_systick_init(k_cpu_cycle_per_tick); @@ -108,7 +109,6 @@ __KERNEL__ void cpu_irq_context_switch(void) __KERNEL__ void cpu_systick_init(k_cycle_t cycle_per_tick) { - port_systick_priority_set(TOS_CFG_CPU_SYSTICK_PRIO); port_systick_config(cycle_per_tick); } diff --git a/arch/linux/posix/gcc/port.c b/arch/linux/posix/gcc/port.c index bb7fb64d..ee199d31 100644 --- a/arch/linux/posix/gcc/port.c +++ b/arch/linux/posix/gcc/port.c @@ -81,17 +81,23 @@ interrupt_manager _int_manager={ }; static uint64_t tick_ms = 0; +static pthread_t main_thread_id; +static pthread_mutex_t cpsr_mutex; + +#define CHECK_IS_MAIN_THREAD(main_thread_id) \ + if(main_thread_id != pthread_self()) return __PORT__ void port_int_disable(void) { sigset_t signal_mask,*manager_mask=NULL; if(_int_manager.count == 0){ + pthread_mutex_lock(&cpsr_mutex); manager_mask = &(_int_manager.signal_mask); + sigfillset(&signal_mask); + _filter_signal(&signal_mask); + pthread_sigmask(SIG_BLOCK,&signal_mask,manager_mask); } _int_manager.count ++; - sigfillset(&signal_mask); - _filter_signal(&signal_mask); - pthread_sigmask(SIG_BLOCK,&signal_mask,manager_mask); } __PORT__ void port_int_enable(void) @@ -102,6 +108,7 @@ __PORT__ void port_int_enable(void) if(_int_manager.count == 0){ sigaddset(&(_int_manager.signal_mask),SIG_RESUME); //ensure SIG_RESUME is blocked _filter_signal(&(_int_manager.signal_mask)); + pthread_mutex_unlock(&cpsr_mutex); pthread_sigmask(SIG_SETMASK,&(_int_manager.signal_mask),NULL); } } @@ -126,10 +133,6 @@ __PORT__ pthread_t port_create_thread(void *arg) __PORT__ void port_sched_start(void) { - _install_signal(SIG_SUSPEND, _handle_suspend_thread); - _install_signal(SIG_RESUME, _handle_resume_thread); - _install_signal(SIG_CONTEXT_SWITCH, _handle_context_switch); - k_curr_task = k_next_task; _resume_task(k_curr_task); @@ -184,8 +187,13 @@ __PORT__ void port_systick_config(uint32_t cycle_per_tick) } } -__PORT__ void port_systick_priority_set(uint32_t prio) +__PORT__ void port_init(void) { + main_thread_id = pthread_self(); + pthread_mutex_init(&cpsr_mutex,NULL); + _install_signal(SIG_SUSPEND, _handle_suspend_thread); + _install_signal(SIG_RESUME, _handle_resume_thread); + _install_signal(SIG_CONTEXT_SWITCH, _handle_context_switch); } __PORT__ void port_delay_ms(uint32_t ms) @@ -224,6 +232,7 @@ __PORT__ void _install_signal(int sig,void (*func)(int)) __PORT__ void _handle_tick_signal() { + CHECK_IS_MAIN_THREAD(main_thread_id); tick_ms ++; if(tos_knl_is_running()) { tos_knl_irq_enter(); diff --git a/arch/linux/posix/gcc/port.h b/arch/linux/posix/gcc/port.h index 97d8ee83..a8f0f28e 100644 --- a/arch/linux/posix/gcc/port.h +++ b/arch/linux/posix/gcc/port.h @@ -89,7 +89,7 @@ __PORT__ void port_irq_context_switch(void); __PORT__ void port_systick_config(uint32_t cycle_per_tick); -__PORT__ void port_systick_priority_set(uint32_t prio); //ok +__PORT__ void port_init(void); __PORT__ pthread_t port_create_thread(void *arg); diff --git a/board/Linux_Posix/mqtt_demo/src/socket_wrapper.c b/board/Linux_Posix/mqtt_demo/src/socket_wrapper.c index 99867790..8ee2b3cd 100644 --- a/board/Linux_Posix/mqtt_demo/src/socket_wrapper.c +++ b/board/Linux_Posix/mqtt_demo/src/socket_wrapper.c @@ -12,12 +12,25 @@ static osMutexId socket_send_lock, socket_recv_lock; osMutexDef(socket_send_lock); osMutexDef(socket_recv_lock); +static osSemaphoreId socket_recv_event; +osSemaphoreDef(socket_recv_event); + int socket_init(void) { socket_send_lock = osMutexCreate(osMutex(socket_send_lock)); socket_recv_lock = osMutexCreate(osMutex(socket_recv_lock)); + socket_recv_event = osSemaphoreCreate(osSemaphore(socket_recv_event),0); - return ((socket_recv_lock != NULL) && (socket_send_lock != NULL)); + return ( + (socket_recv_event != NULL) && + (socket_recv_lock != NULL) && + (socket_send_lock != NULL) + ); +} + +void io_signal_handle(int signal) +{ + osSemaphoreRelease(socket_recv_event); } int socket_connect(const char *ip, const char *port, sal_proto_t proto) @@ -26,6 +39,7 @@ int socket_connect(const char *ip, const char *port, sal_proto_t proto) .sin_family = AF_INET, .sin_port = htons(atoi(port))}; int socket_proto = 0; + struct sigaction sig_install; inet_pton(AF_INET, ip, &addr.sin_addr); @@ -43,6 +57,19 @@ int socket_connect(const char *ip, const char *port, sal_proto_t proto) } int socket_id = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + + fcntl( socket_id, __F_SETOWN, getpid() ); + fcntl( socket_id, __F_SETSIG, SIGIO ); + fcntl( socket_id, F_SETFL, O_ASYNC ); + + sig_install.sa_flags = 0; + sig_install.sa_handler = io_signal_handle; + sigfillset( &sig_install.sa_mask ); + + if ( 0 != sigaction( SIGIO, &sig_install, NULL ) ) + { + printf( "socket problem installing %d\n",SIGIO ); + } connect(socket_id, &addr, sizeof(addr)); return socket_id; @@ -63,7 +90,7 @@ int socket_send(int sock, const void *buf, size_t len) } if (send_len != len) { - osDelay(5); + osSemaphoreWait(socket_recv_event,100); } } while (len != send_len); osMutexRelease(socket_send_lock); @@ -85,7 +112,7 @@ int socket_recv(int sock, void *buf, size_t len) } if (recv_len != len) { - osDelay(5); + osSemaphoreWait(socket_recv_event,100); } } while (len != recv_len); osMutexRelease(socket_recv_lock);