/* $Id$ * $URL$ * * thread handling (mutex, shmem, ...) * * Copyright (C) 2004 Michael Reinelt * Copyright (C) 2004 The LCD4Linux Team * * parts of this code are based on the old XWindow driver which is * Copyright (C) 2000 Herbert Rosmanith * * This file is part of LCD4Linux. * * LCD4Linux is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * LCD4Linux is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* * exported functions: * * int mutex_create (void); * creates a mutex and treturns its ID * * void mutex_lock (int semid); * try to lock a mutex * * void mutex_unlock (int semid); * unlock a mutex * * void mutex_destroy (int semid); * release a mutex * * * int shm_create (void **buffer, int size); * create shared memory segment * * void shm_destroy (int shmid, void *buffer) ; * release shared memory segment * * int thread_create (char *name, void (*thread)(void *data), void *data); * create a new thread * */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include "debug.h" #include "thread.h" #ifdef WITH_DMALLOC #include #endif int thread_argc; char **thread_argv; /* glibc 2.1 requires defining semun ourselves */ #ifdef _SEM_SEMUN_UNDEFINED union semun { int val; struct semid_ds *buf; unsigned short int *array; struct seminfo *__buf; }; #endif int mutex_create(void) { int semid; union semun semun; semid = semget(IPC_PRIVATE, 1, 0); if (semid == -1) { error("fatal error: semget() failed: %s", strerror(errno)); return -1; } semun.val = 1; semctl(semid, 0, SETVAL, semun); return semid; } void mutex_lock(const int semid) { struct sembuf sembuf; sembuf.sem_num = 0; sembuf.sem_op = -1; sembuf.sem_flg = 0; semop(semid, &sembuf, 1); } void mutex_unlock(const int semid) { struct sembuf sembuf; sembuf.sem_num = 0; sembuf.sem_op = 1; sembuf.sem_flg = 0; semop(semid, &sembuf, 1); } void mutex_destroy(const int semid) { union semun arg; semctl(semid, 0, IPC_RMID, arg); } int shm_create(void **buffer, const int size) { int shmid; shmid = shmget(IPC_PRIVATE, size, SHM_R | SHM_W); if (shmid == -1) { error("fatal error: shmget() failed: %s", strerror(errno)); return -1; } *buffer = shmat(shmid, NULL, 0); if (*buffer == NULL) { error("fatal error: shmat() failed: %s", strerror(errno)); return -1; } return shmid; } void shm_destroy(const int shmid, const void *buffer) { shmdt(buffer); shmctl(shmid, IPC_RMID, NULL); } int thread_create(const char *name, void (*thread) (void *data), void *data) { pid_t pid, ppid; ppid = getpid(); switch (pid = fork()) { case -1: error("fatal error: fork(%s) failed: %s", name, strerror(errno)); return -1; case 0: info("thread %s starting...", name); if (thread_argc > 0) { strncpy(thread_argv[0], name, strlen(thread_argv[0])); } thread(data); info("thread %s ended.", name); exit(0); default: info("forked process %d for thread %s", pid, name); } return pid; } int thread_destroy(const int pid) { return kill(pid, SIGKILL); } 29[lcd4linux @ 2004-08-29 13:03:40 by reinelt]reinelt9-55/+709 2004-07-14[lcd4linux @ 2004-07-14 04:44:44 by reinelt]reinelt2-4/+19 2004-06-29[lcd4linux @ 2004-06-29 04:49:30 by reinelt]reinelt1-7/+47 2004-06-26[lcd4linux @ 2004-06-26 12:04:59 by reinelt]reinelt69-139/+413 2004-06-26[lcd4linux @ 2004-06-26 09:27:20 by reinelt]reinelt71-1842/+2274 2004-06-26[lcd4linux @ 2004-06-26 06:12:14 by reinelt]reinelt7-111/+562 2004-06-24[lcd4linux @ 2004-06-24 20:18:08 by nicowallmeier]nicowallmeier1-2/+5 2004-06-20[lcd4linux @ 2004-06-20 10:12:27 by reinelt]reinelt1-0/+110 2004-06-20[lcd4linux @ 2004-06-20 10:09:52 by reinelt]reinelt51-377/+569 2004-06-19[lcd4linux @ 2004-06-19 08:20:19 by reinelt]reinelt2-7/+17 2004-06-17[lcd4linux @ 2004-06-17 10:58:57 by reinelt]reinelt3-76/+84 2004-06-17[lcd4linux @ 2004-06-17 06:23:39 by reinelt]reinelt19-403/+633 2004-06-13[lcd4linux @ 2004-06-13 01:12:52 by reinelt]reinelt3-18/+34 2004-06-09[lcd4linux @ 2004-06-09 06:40:29 by reinelt]reinelt3-11/+35 2004-06-08[lcd4linux @ 2004-06-08 21:46:38 by reinelt]reinelt5-31/+131 2004-06-08[lcd4linux @ 2004-06-08 12:35:24 by reinelt]reinelt6-882/+474 2004-06-07[lcd4linux @ 2004-06-07 07:02:13 by reinelt]reinelt1-0/+33 2004-06-07[lcd4linux @ 2004-06-07 06:56:55 by reinelt]reinelt5-7/+149 2004-06-06[lcd4linux @ 2004-06-06 06:51:59 by reinelt]reinelt16-52/+135 2004-06-05[lcd4linux @ 2004-06-05 14:56:48 by reinelt]reinelt3-24/+37 2004-06-05[lcd4linux @ 2004-06-05 06:41:39 by reinelt]reinelt11-42/+87 2004-06-05[lcd4linux @ 2004-06-05 06:13:11 by reinelt]reinelt9-69/+327 2004-06-02[lcd4linux @ 2004-06-02 10:09:22 by reinelt]reinelt4-180/+183 2004-06-02[lcd4linux @ 2004-06-02 09:41:19 by reinelt]reinelt18-114/+211 2004-06-02[lcd4linux @ 2004-06-02 05:56:25 by reinelt]reinelt1-2/+6 2004-06-02[lcd4linux @ 2004-06-02 05:35:55 by reinelt]reinelt1-0/+20 2004-06-02[lcd4linux @ 2004-06-02 05:27:59 by reinelt]reinelt26-0/+2405 2004-06-02[lcd4linux @ 2004-06-02 05:14:16 by reinelt]reinelt2-17/+31 2004-06-01[lcd4linux @ 2004-06-01 06:45:28 by reinelt]reinelt18-71/+241 2004-06-01[lcd4linux @ 2004-06-01 06:04:25 by reinelt]reinelt2-25/+25 2004-05-31[lcd4linux @ 2004-05-31 21:23:16 by reinelt]reinelt1-37/+49 2004-05-31[lcd4linux @ 2004-05-31 21:05:13 by reinelt]reinelt5-77/+111 2004-05-31[lcd4linux @ 2004-05-31 16:39:05 by reinelt]reinelt11-180/+535 2004-05-31[lcd4linux @ 2004-05-31 06:27:34 by reinelt]reinelt1-0/+40 2004-05-31[lcd4linux @ 2004-05-31 06:24:42 by reinelt]reinelt2-7/+29 2004-05-31[lcd4linux @ 2004-05-31 05:38:02 by reinelt]reinelt8-42/+94 2004-05-31[lcd4linux @ 2004-05-31 01:31:01 by andy-b]andy-b1-10/+12 2004-05-30[lcd4linux @ 2004-05-30 08:25:50 by reinelt]reinelt2-30/+92 2004-05-29[lcd4linux @ 2004-05-29 23:30:20 by reinelt]reinelt1-1/+9 2004-05-29[lcd4linux @ 2004-05-29 15:53:28 by reinelt]reinelt3-9/+25 2004-05-29[lcd4linux @ 2004-05-29 01:07:56 by reinelt]reinelt2-21/+25 2004-05-29[lcd4linux @ 2004-05-29 00:27:14 by reinelt]reinelt8-11/+214 2004-05-28[lcd4linux @ 2004-05-28 14:38:10 by reinelt]reinelt2-1/+41