/** * @file actl_thread.h * @brief Thread Queue¿¡ ÇÊ¿äÇÑ ±âº»ÀûÀÎ Interface * * ÁÖÀÇÇÒ Á¡Àº ÀÌ´Â Kernel¿µ¿ª¿¡¼­ »ç¿ëÇÏ´Â°Ç Áö¿øÇÏÁö ¾Ê´Â´Ù. * ¹Ýµå½Ã User¿µ¿ª¿¡¼­¸¸ »ç¿ëÇØ¾ß ÇÑ´Ù. * * @date Apr. 4, 2003 * @author acetcom */ #ifndef __ACTL_THREAD_QUEUE_H__ #define __ACTL_THREAD_QUEUE_H__ #include #include #include #include #define ACTL_THREAD_QUEUE_SUCCESS 0 #define ACTL_THREAD_QUEUE_FAILURE 100 #define ACTL_THREAD_QUEUE_FAILURE_TIMEOUT (ACTL_THREAD_QUEUE_FAILURE+1) #define ACTL_THREAD_QUEUE_TRUE 1 #define ACTL_THREAD_QUEUE_FALSE 0 #define ACTL_USEC_PER_SEC (unsigned long long)(1000000) #define ACTL_MSEC_PER_SEC (unsigned long long)(1000) typedef struct actl_thread_queue actl_thread_queue; /** * @ingroup ACTL_THREAD_QUEUE * @struct actl_thread_queue * @brief Thread-SafeÅ¥¸¦ À§ÇÑ ±âº» ±¸Á¶Ã¼ÀÌ´Ù. * * °íÁ¤ÀûÀÎ ±æÀ̸¦ °¡Áø µ¥ÀÌÅÍ´Â ¸ðµÎ actl_thread_queue·Î * ¸ðµÎ ±¸ÇöÇÒ ¼ö ÀÖ´Ù. * * @code * * #include * * actl_thread_queue q; * * #define ACTL_THREAD_QUEUE_NUMBER 50 * * void *producer_func(void *arg) * { * int n = 0; * * while(1) * { * printf("put queue[%d] = %d\n", pthread_self(), n); * fflush(stdout); * q.put_queue(&q, &n, 2000 * ACTL_MSEC_PER_SEC); * ++n; * } * } * * void *consumer_func(void *arg) * { * int n; * while(1) * { * q.get_queue(&q, &n, 2000 * ACTL_MSEC_PER_SEC); * printf("get queue[%d] = %d\n", pthread_self(), n); * fflush(stdout); * } * } * * int main() * { * pthread_t t1, t2; * int i; * * if (actl_thread_queue_init(&q) != ACTL_THREAD_QUEUE_SUCCESS) * { * fprintf(stderr, "ACTL Thread Initializing Failed\n"); * exit(-1); * } * * q.create(&q, 3, sizeof(int)); * q.destroy(&q); * * pthread_create(&t2, NULL, consumer_func, NULL); * for (i = 0; i < ACTL_THREAD_QUEUE_NUMBER; i++) * { * pthread_create(&t1, NULL, producer_func, NULL); * } * * pthread_join(t1, NULL); * pthread_join(t2, NULL); * } * @endcode */ struct actl_thread_queue { /** * @brief ÇöÀç Å¥¿¡ µé¾îÀÖ´Â µ¥ÀÌÅ͸¦ º¸°üÇϱâ À§ÇÑ ¸Þ¸ð¸® */ char **queue_buffer; /** * @brief Å¥¿¡ ³Ö°í »©³¾ µ¥ÀÌÅÍÀÇ ±æÀÌ */ int obj_size; /** * @brief Å¥ÀÇ Å©±â */ int queue_size; /** * @brief Å¥ÀÇ Àбâ À§Ä¡ * get_queue¿¡ ÀÇÇØ Å¥¸¦ ÀÐÀ» ¶§, Àд À§Ä¡¸¦ °¡¸®Å²´Ù. */ int get_position; /** * @brief Å¥ÀÇ ¾²±â À§Ä¡ * put_queue¿¡ ÀÇÇØ Å¥¸¦ ¾µ ¶§, ¾²´Â À§Ä¡¸¦ °¡¸®Å²´Ù. */ int put_position; /** * @brief Å¥°¡ Â÷ ÀÖ´ÂÁö¸¦ ±â·ÏÇÏ´Â Ç÷¡±× */ int is_full; /** * @brief Å¥°¡ ºñ¾î ÀÖ´ÂÁö¸¦ ±â·ÏÇÏ´Â Ç÷¡Å© */ int is_empty; /** * @brief ¿©·¯ Thread°¡ put_queueÇÒ ¶§, ÇÊ¿äÇÑ Lock */ pthread_mutex_t lock1; /** * @brief Condition VariableÀ» À§ÇÑ Lock */ pthread_mutex_t lock2; /** * @brief Å¥°¡ ä¿öÁ® ÀÖÀ» ¶§, ±â´Ù¸®±â À§ÇÑ Condition Variable */ pthread_cond_t cond1; /** * @brief Å¥°¡ ºñ¿öÁ® ÀÖÀ» ¶§, ±â´Ù¸®±â À§ÇÑ Condition Variable */ pthread_cond_t cond2; /** * @brief Å¥ÀÇ Å©±â¸¦ Á¤ÇÏ°í, Àбâ À§Ä¡¿Í ¾²±â À§Ä¡ µîÀ» ÃʱâÈ­ ÇÑ´Ù. * * @param queue_size Å¥ÀÇ Å©±â¸¦ ÁöÁ¤ÇÑ´Ù * @param obj_size Å¥¿¡ ³Ö°í »©³¾ µ¥ÀÌÅÍÀÇ ±æÀÌ * @return ¼º°øÇϸé ACTL_THREAD_QUEUE_SUCCESS¸¦ ¸®ÅÏÇÑ´Ù. */ int (*create)(actl_thread_queue *q, int queue_size, int obj_size); /** * @brief ¸Þ¸ð¸®µéÀ» ÇØÁ¦ÇÑ´Ù. * @return ¼º°øÇϸé ACTL_THREAD_QUEUE_SUCCESS¸¦ ¸®ÅÏÇÑ´Ù. */ int (*destroy)(actl_thread_queue *q); /** * @brief »ç¿ëÀÚ°¡ Å¥¿¡ µ¥ÀÌÅ͸¦ ³ÖÀ» ¶§, À̸¦ ÀÌ¿ëÇÑ´Ù. * * @param buf Å¥¿¡ ³ÖÀ» µ¥ÀÌÅÍ * @param sec Å¥°¡ ´Ù Âù °æ¿ì ±â´Ù¸®´Â ½Ã°£(´ÜÀ§ : 1/1000000ÃÊ) * @return Å¥°¡ Ç®ÀÌ°í ±â´Ù¸®´Â ½Ã°£ÀÌ ÃÊ°úÇϸé false¸¦ ¸®ÅÏÇÑ´Ù. */ int (*put_queue)(actl_thread_queue *q, void *buf, unsigned long long usec); /** * @brief »ç¿ëÀÚ°¡ Å¥·ÎºÎÅÍ µ¥ÀÌÅ͸¦ °¡Á® ¿Ã¶§, À̸¦ ÀÌ¿ëÇÑ´Ù. * * @param buf Å¥·Î ºÎÅÍ °¡Á®¿Â µ¥ÀÌÅ͸¦ ÀúÀåÇÏ´Â ¸Þ¸ð¸® * @param sec Å¥°¡ ºñ¾î ÀÖ´Â °æ¿ì ±â´Ù¸®´Â ½Ã°£(´ÜÀ§ : 1/1000000ÃÊ) * @return Å¥°¡ Ç®ÀÌ°í ±â´Ù¸®´Â ½Ã°£ÀÌ ÃÊ°úÇϸé false¸¦ ¸®ÅÏÇÑ´Ù. */ int (*get_queue)(actl_thread_queue *q, void *buf, unsigned long long usec); }; /** * @ingroup ACTL_THREAD_QUEUE * @brief Thread Queue°ü·Ã Interface¸¦ »ç¿ëÇϱâ À§ÇØ ¹Ýµå½Ã È£ÃâÇØ¾ß ÇÑ´Ù. * @return ¼º°øÇϸé ACTL_THREAD_QUEUE_SUCCESS¸¦ ¸®ÅÏÇÑ´Ù. */ int actl_thread_queue_init(actl_thread_queue *q); /** * @ingroup ACTL_THREAD_QUEUE * @brief Thread Queue°ü·Ã InterfaceÀÇ »ç¿ëÀ» ¸¶Ä¡¸é È£ÃâÇÑ´Ù. * @return ¼º°øÇϸé ACTL_THREAD_QUEUE_SUCCESS¸¦ ¸®ÅÏÇÑ´Ù. */ int actl_thread_queue_cleanup(actl_thread_queue *q); #endif