struct msgqueue * msgqueue_create( uint32_t size ) { struct msgqueue * self = NULL; self = (struct msgqueue *)malloc( sizeof(struct msgqueue) ); if ( self ) { self->popfd = -1; self->pushfd = -1; pthread_mutex_init( &self->lock, 0 ); if ( QUEUE_INIT(taskqueue)(&self->queue, size) != 0 ) { msgqueue_destroy( self ); self = NULL; } else { int32_t rc = -1; int32_t fds[2] = { -1, -1 }; rc = pipe( fds ); //rc = socketpair( AF_UNIX, SOCK_STREAM, 0, fds ); if ( rc == -1 ) { msgqueue_destroy( self ); self = NULL; } else { self->popfd = fds[0]; self->pushfd = fds[1]; #ifdef O_NOATIME // linux在读pipe的时候会更新访问时间, touch_atime(), 这个的开销也不小 fcntl( self->popfd, F_SETFL, O_NOATIME ); #endif } } } return self; }
int32_t iothread_stop( struct iothread * self ) { if ( self->queue ) { msgqueue_destroy( self->queue ); self->queue = NULL; } if ( self->cmdevent ) { evsets_del( self->sets, self->cmdevent ); event_destroy( self->cmdevent ); self->cmdevent = NULL; } if ( self->sets ) { evsets_destroy( self->sets ); self->sets = NULL; } return 0; }