comparison gtk/dw.c @ 558:05060ce4d6ae

Fixes for the new shared memory and named event code.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 17 Apr 2004 06:04:40 +0000
parents 1a210e2f214b
children c0a708e2cba3
comparison
equal deleted inserted replaced
557:1a210e2f214b 558:05060ce4d6ae
16 #include <unistd.h> 16 #include <unistd.h>
17 #include <errno.h> 17 #include <errno.h>
18 #include <sys/time.h> 18 #include <sys/time.h>
19 #include <dirent.h> 19 #include <dirent.h>
20 #include <sys/stat.h> 20 #include <sys/stat.h>
21 #include <signal.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/mman.h>
25 #include <fcntl.h>
26 #include <unistd.h>
21 #include "config.h" 27 #include "config.h"
22 #include <gdk/gdkkeysyms.h> 28 #include <gdk/gdkkeysyms.h>
23 #ifdef USE_IMLIB 29 #ifdef USE_IMLIB
24 #include <gdk_imlib.h> 30 #include <gdk_imlib.h>
25 #endif 31 #endif
7533 struct sockaddr_un un; 7539 struct sockaddr_un un;
7534 int ev, *tmpsock = (int *)malloc(sizeof(int)*2); 7540 int ev, *tmpsock = (int *)malloc(sizeof(int)*2);
7535 DWTID dwthread; 7541 DWTID dwthread;
7536 7542
7537 if(!tmpsock) 7543 if(!tmpsock)
7538 return DB_EVENT_NO_MEM; 7544 return NULL;
7539 7545
7540 tmpsock[0] = socket(AF_UNIX, SOCK_STREAM, 0); 7546 tmpsock[0] = socket(AF_UNIX, SOCK_STREAM, 0);
7541 ev = socket(AF_UNIX, SOCK_STREAM, 0); 7547 ev = socket(AF_UNIX, SOCK_STREAM, 0);
7542 memset(&un, 0, sizeof(un)); 7548 memset(&un, 0, sizeof(un));
7543 un.sun_family=AF_UNIX; 7549 un.sun_family=AF_UNIX;
7562 if(tmpsock[1] > -1) 7568 if(tmpsock[1] > -1)
7563 close(tmpsock[1]); 7569 close(tmpsock[1]);
7564 if(ev > -1) 7570 if(ev > -1)
7565 close(ev); 7571 close(ev);
7566 free(tmpsock); 7572 free(tmpsock);
7567 return 0; 7573 return NULL;
7568 } 7574 }
7569 7575
7570 /* Create a thread to handle this event semaphore */ 7576 /* Create a thread to handle this event semaphore */
7571 pthread_create(&dwthread, NULL, (void *)_handle_sem, (void *)tmpsock); 7577 pthread_create(&dwthread, NULL, (void *)_handle_sem, (void *)tmpsock);
7572 return ev; 7578 return (HEV)ev;
7573 } 7579 }
7574 7580
7575 /* Open an already existing named event semaphore. 7581 /* Open an already existing named event semaphore.
7576 * Parameters: 7582 * Parameters:
7577 * eve: Pointer to an event handle to receive handle. 7583 * eve: Pointer to an event handle to receive handle.
7581 HEV dw_named_event_get(char *name) 7587 HEV dw_named_event_get(char *name)
7582 { 7588 {
7583 struct sockaddr_un un; 7589 struct sockaddr_un un;
7584 int ev = socket(AF_UNIX, SOCK_STREAM, 0); 7590 int ev = socket(AF_UNIX, SOCK_STREAM, 0);
7585 if(ev < 0) 7591 if(ev < 0)
7586 return 0; 7592 return NULL;
7587 7593
7588 un.sun_family=AF_UNIX; 7594 un.sun_family=AF_UNIX;
7589 mkdir("/tmp/.dw", S_IWGRP|S_IWOTH); 7595 mkdir("/tmp/.dw", S_IWGRP|S_IWOTH);
7590 strcpy(un.sun_path, "/tmp/.dw/"); 7596 strcpy(un.sun_path, "/tmp/.dw/");
7591 strcat(un.sun_path, name); 7597 strcat(un.sun_path, name);
7592 connect(ev, (struct sockaddr *)&un, sizeof(un)); 7598 connect(ev, (struct sockaddr *)&un, sizeof(un));
7593 return ev; 7599 return (HEV)ev;
7594 } 7600 }
7595 7601
7596 /* Resets the event semaphore so threads who call wait 7602 /* Resets the event semaphore so threads who call wait
7597 * on this semaphore will block. 7603 * on this semaphore will block.
7598 * Parameters: 7604 * Parameters:
7602 int dw_named_event_reset(HEV eve) 7608 int dw_named_event_reset(HEV eve)
7603 { 7609 {
7604 /* signal reset */ 7610 /* signal reset */
7605 char tmp = (char)0; 7611 char tmp = (char)0;
7606 7612
7607 if(eve < 0) 7613 if((int)eve < 0)
7608 return 0; 7614 return 0;
7609 7615
7610 if(write(eve, &tmp, 1) == 1) 7616 if(write((int)eve, &tmp, 1) == 1)
7611 return 0; 7617 return 0;
7612 return 1; 7618 return 1;
7613 } 7619 }
7614 7620
7615 /* Sets the posted state of an event semaphore, any threads 7621 /* Sets the posted state of an event semaphore, any threads
7622 { 7628 {
7623 7629
7624 /* signal post */ 7630 /* signal post */
7625 char tmp = (char)1; 7631 char tmp = (char)1;
7626 7632
7627 if(eve < 0) 7633 if((int)eve < 0)
7628 return 0; 7634 return 0;
7629 7635
7630 if(write(eve, &tmp, 1) == 1) 7636 if(write((int)eve, &tmp, 1) == 1)
7631 return 0; 7637 return 0;
7632 return 1; 7638 return 1;
7633 } 7639 }
7634 7640
7635 /* Waits on the specified semaphore until it becomes 7641 /* Waits on the specified semaphore until it becomes
7645 fd_set rd; 7651 fd_set rd;
7646 struct timeval tv, *useme; 7652 struct timeval tv, *useme;
7647 int retval = 0; 7653 int retval = 0;
7648 char tmp; 7654 char tmp;
7649 7655
7650 if(eve < 0) 7656 if((int)eve < 0)
7651 return DB_EVENT_NON_INIT; 7657 return DW_ERROR_NON_INIT;
7652 7658
7653 /* Set the timout or infinite */ 7659 /* Set the timout or infinite */
7654 if(timeout == -1) 7660 if(timeout == -1)
7655 useme = NULL; 7661 useme = NULL;
7656 else 7662 else
7660 7666
7661 useme = &tv; 7667 useme = &tv;
7662 } 7668 }
7663 7669
7664 FD_ZERO(&rd); 7670 FD_ZERO(&rd);
7665 FD_SET(eve, &rd); 7671 FD_SET((int)eve, &rd);
7666 7672
7667 /* Signal wait */ 7673 /* Signal wait */
7668 tmp = (char)2; 7674 tmp = (char)2;
7669 write(eve, &tmp, 1); 7675 write((int)eve, &tmp, 1);
7670 7676
7671 retval = select(eve+1, &rd, NULL, NULL, useme); 7677 retval = select((int)eve+1, &rd, NULL, NULL, useme);
7672 7678
7673 /* Signal done waiting. */ 7679 /* Signal done waiting. */
7674 tmp = (char)3; 7680 tmp = (char)3;
7675 write(eve, &tmp, 1); 7681 write((int)eve, &tmp, 1);
7676 7682
7677 if(retval == 0) 7683 if(retval == 0)
7678 return DW_EVENT_TIMEOUT; 7684 return DW_ERROR_TIMEOUT;
7679 else if(retval == -1) 7685 else if(retval == -1)
7680 return DW_EVENT_INTERRUPT; 7686 return DW_ERROR_INTERRUPT;
7681 7687
7682 /* Clear the entry from the pipe so 7688 /* Clear the entry from the pipe so
7683 * we don't loop endlessly. :) 7689 * we don't loop endlessly. :)
7684 */ 7690 */
7685 read(eve, &tmp, 1); 7691 read((int)eve, &tmp, 1);
7686 return 0; 7692 return 0;
7687 } 7693 }
7688 7694
7689 /* Release this semaphore, if there are no more open 7695 /* Release this semaphore, if there are no more open
7690 * handles on this semaphore the semaphore will be destroyed. 7696 * handles on this semaphore the semaphore will be destroyed.
7695 int dw_named_event_close(HEV eve) 7701 int dw_named_event_close(HEV eve)
7696 { 7702 {
7697 /* Finally close the domain socket, 7703 /* Finally close the domain socket,
7698 * cleanup will continue in _handle_sem. 7704 * cleanup will continue in _handle_sem.
7699 */ 7705 */
7700 close(eve); 7706 close((int)eve);
7701 return 0; 7707 return 0;
7702 } 7708 }
7703 7709
7704 /* 7710 /*
7705 * Setup thread independent color sets. 7711 * Setup thread independent color sets.
7726 * name: A string pointer to a unique memory name. 7732 * name: A string pointer to a unique memory name.
7727 */ 7733 */
7728 HSHM dw_named_memory_new(void **dest, int size, char *name) 7734 HSHM dw_named_memory_new(void **dest, int size, char *name)
7729 { 7735 {
7730 char namebuf[1024]; 7736 char namebuf[1024];
7731 HSHM handle; 7737 struct _dw_unix_shm *handle = malloc(sizeof(struct _dw_unix_shm));
7732 7738
7733 mkdir("/tmp/.dw", S_IWGRP|S_IWOTH); 7739 mkdir("/tmp/.dw", S_IWGRP|S_IWOTH);
7734 sprintf(namebuf, "/tmp/.dw/%s", name); 7740 sprintf(namebuf, "/tmp/.dw/%s", name);
7735 7741
7736 if((handle->fd = open(namebuf, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) 7742 if((handle->fd = open(namebuf, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0)
7737 return 0; 7743 {
7744 free(handle);
7745 return NULL;
7746 }
7738 7747
7739 ftruncate(handle->fd, size); 7748 ftruncate(handle->fd, size);
7740 7749
7741 /* attach the shared memory segment to our process's address space. */ 7750 /* attach the shared memory segment to our process's address space. */
7742 *dest = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, 0); 7751 *dest = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, 0);
7743 7752
7744 if(*dest == MAP_FAILED) 7753 if(*dest == MAP_FAILED)
7745 { 7754 {
7746 close(handle->fd); 7755 close(handle->fd);
7747 *dest = NULL; 7756 *dest = NULL;
7748 return 0; 7757 free(handle);
7758 return NULL;
7749 } 7759 }
7750 7760
7751 handle->size = size; 7761 handle->size = size;
7752 handle->sid = getsid(0); 7762 handle->sid = getsid(0);
7753 handle->path = strdup(namebuf); 7763 handle->path = strdup(namebuf);
7763 * name: A string pointer to a unique memory name. 7773 * name: A string pointer to a unique memory name.
7764 */ 7774 */
7765 HSHM dw_named_memory_get(void **dest, int size, char *name) 7775 HSHM dw_named_memory_get(void **dest, int size, char *name)
7766 { 7776 {
7767 char namebuf[1024]; 7777 char namebuf[1024];
7768 HSHM handle; 7778 struct _dw_unix_shm *handle = malloc(sizeof(struct _dw_unix_shm));
7769 7779
7770 mkdir("/tmp/.dw", S_IWGRP|S_IWOTH); 7780 mkdir("/tmp/.dw", S_IWGRP|S_IWOTH);
7771 sprintf(namebuf, "/tmp/.dw/%s", name); 7781 sprintf(namebuf, "/tmp/.dw/%s", name);
7772 7782
7773 if((handle->fd = open(namebuf, O_RDWR)) < 0) 7783 if((handle->fd = open(namebuf, O_RDWR)) < 0)
7774 return -1; 7784 {
7785 free(handle);
7786 return NULL;
7787 }
7775 7788
7776 /* attach the shared memory segment to our process's address space. */ 7789 /* attach the shared memory segment to our process's address space. */
7777 *dest = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, 0); 7790 *dest = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, 0);
7778 7791
7779 if(*dest == MAP_FAILED) 7792 if(*dest == MAP_FAILED)
7780 { 7793 {
7781 close(handle->fd); 7794 close(handle->fd);
7782 *dest = NULL; 7795 *dest = NULL;
7783 return 0; 7796 free(handle);
7797 return NULL;
7784 } 7798 }
7785 7799
7786 handle->size = size; 7800 handle->size = size;
7787 handle->sid = -1; 7801 handle->sid = -1;
7788 handle->path = NULL; 7802 handle->path = NULL;
7796 * handle: Handle obtained from DB_named_memory_allocate. 7810 * handle: Handle obtained from DB_named_memory_allocate.
7797 * ptr: The memory address aquired with DB_named_memory_allocate. 7811 * ptr: The memory address aquired with DB_named_memory_allocate.
7798 */ 7812 */
7799 int dw_named_memory_free(HSHM handle, void *ptr) 7813 int dw_named_memory_free(HSHM handle, void *ptr)
7800 { 7814 {
7801 int rc = munmap(ptr, handle.size); 7815 struct _dw_unix_shm *h = handle;
7802 7816 int rc = munmap(ptr, h->size);
7803 close(handle.fd); 7817
7804 if(handle.path) 7818 close(h->fd);
7819 if(h->path)
7805 { 7820 {
7806 /* Only remove the actual file if we are the 7821 /* Only remove the actual file if we are the
7807 * creator of the file. 7822 * creator of the file.
7808 */ 7823 */
7809 if(handle.sid != -1 && handle.sid == getsid(0)) 7824 if(h->sid != -1 && h->sid == getsid(0))
7810 remove(handle.path); 7825 remove(h->path);
7811 free(handle.path); 7826 free(h->path);
7812 } 7827 }
7813 return rc; 7828 return rc;
7814 } 7829 }
7815 /* 7830 /*
7816 * Creates a new thread with a starting point of func. 7831 * Creates a new thread with a starting point of func.