-/* $Id: mmpop3d.c,v 1.40 2005/03/05 15:33:33 mmondor Exp $ */
+/* $Id: mmpop3d.c,v 1.40.2.1 2005/11/24 07:13:39 mmondor Exp $ */
/*
* Copyright (C) 2001-2004, Matthew Mondor
#include <time.h>
#include <ctype.h>
-#include <pth.h>
+
+#include <pthread.h>
#include <mmtypes.h>
#include <mmreadcfg.h>
#include <mmstring.h>
#include <mmstat.h>
+#include <mm_pthread_sleep.h>
+
#include "mmpop3d.h"
MMCOPYRIGHT("@(#) Copyright (c) 2001-2004\n\
\tMatthew Mondor. All rights reserved.\n");
-MMRCSID("$Id: mmpop3d.c,v 1.40 2005/03/05 15:33:33 mmondor Exp $");
+MMRCSID("$Id: mmpop3d.c,v 1.40.2.1 2005/11/24 07:13:39 mmondor Exp $");
static int LOGLEVEL;
/* Pool used to allocate clientenv nodes */
-static pth_mutex_t clenv_lock;
+static pthread_mutex_t clenv_lock = PTHREAD_MUTEX_INITIALIZER;
static pool_t clenv_pool;
/* Pool used to optimize creating/destroying mmfd mutexes */
-static pth_mutex_t mutexes_lock;
+static pthread_mutex_t mutexes_lock = PTHREAD_MUTEX_INITIALIZER;
static pool_t mutexes_pool;
/* Used for fast command lookup */
static fdfuncs gfdf = {
malloc,
free,
- pth_poll,
- pth_read,
- pth_write,
- pth_sleep,
- pth_usleep,
- _pth_mutex_create,
- _pth_mutex_destroy,
- _pth_mutex_lock,
- _pth_mutex_unlock,
- _pth_thread_yield,
- _pth_eintr
+ poll,
+ read,
+ write,
+ pthread_sleep,
+ pthread_usleep,
+ thread_mutex_create,
+ thread_mutex_destroy,
+ thread_mutex_lock,
+ thread_mutex_unlock,
+ NULL,
+ thread_eintr
};
{NULL, 0}
};
struct mmsql_threadsupport mmsqlfuncs = {
- _pth_mutex_create,
- _pth_mutex_destroy,
- _pth_mutex_lock,
- _pth_mutex_unlock,
- _pth_thread_yield,
- _pth_thread_sleep
+ thread_mutex_create,
+ thread_mutex_destroy,
+ thread_mutex_lock,
+ thread_mutex_unlock,
+ NULL,
+ pthread_sleep
};
mmstat_t vstat;
async_init(afuncs, CONF.ASYNC_PROCESSES, uid, gids, ngids);
/* Things which shouldn't be part of the async pool processes */
- pth_init();
- async_init_pth();
- pth_mutex_init(&clenv_lock);
- pth_mutex_init(&mutexes_lock);
+ async_init_pthread();
/* Allocate necessary pools */
/* Client nodes */
{
clientenv *clenv;
- pth_mutex_acquire(&clenv_lock, FALSE, NULL);
+ pthread_mutex_lock(&clenv_lock);
clenv = (clientenv *)pool_alloc(&clenv_pool, TRUE);
- pth_mutex_release(&clenv_lock);
+ pthread_mutex_unlock(&clenv_lock);
if (clenv != NULL) {
mmstat_init(&clenv->pstat, TRUE, FALSE);
if (clenv->index != NULL)
free(clenv->index);
- pth_mutex_acquire(&clenv_lock, FALSE, NULL);
+ pthread_mutex_lock(&clenv_lock);
pool_free((pnode_t *)clenv);
- pth_mutex_release(&clenv_lock);
+ pthread_mutex_unlock(&clenv_lock);
return (NULL);
}
static void *
-_pth_mutex_create(void)
+thread_mutex_create(void)
{
struct mutexnode *mnod;
- pth_mutex_acquire(&mutexes_lock, FALSE, NULL);
+ pthread_mutex_lock(&mutexes_lock);
mnod = (struct mutexnode *)pool_alloc(&mutexes_pool, FALSE);
- pth_mutex_release(&mutexes_lock);
+ pthread_mutex_unlock(&mutexes_lock);
if (mnod != NULL)
- pth_mutex_init(&mnod->mutex);
+ pthread_mutex_init(&mnod->mutex, NULL);
return ((void *)mnod);
}
static void *
-_pth_mutex_destroy(void *mtx)
+thread_mutex_destroy(void *mtx)
{
- /* struct mutexnode *mnod = mtx; */
+ struct mutexnode *mnod = mtx;
- /* pth_mutex_destroy(&mnod->mutex); */
- pth_mutex_acquire(&mutexes_lock, FALSE, NULL);
+ pthread_mutex_destroy(&mnod->mutex);
+ pthread_mutex_lock(&mutexes_lock);
pool_free(mtx);
- pth_mutex_release(&mutexes_lock);
+ pthread_mutex_unlock(&mutexes_lock);
return (NULL);
}
static void
-_pth_mutex_lock(void *mtx)
+thread_mutex_lock(void *mtx)
{
struct mutexnode *mnod = mtx;
- pth_mutex_acquire(&mnod->mutex, FALSE, NULL);
+ pthread_mutex_lock(&mnod->mutex);
}
static void
-_pth_mutex_unlock(void *mtx)
+thread_mutex_unlock(void *mtx)
{
struct mutexnode *mnod = mtx;
- pth_mutex_release(&mnod->mutex);
-}
-
-
-static void
-_pth_thread_yield(void)
-{
- pth_yield(NULL);
-}
-
-
-static void
-_pth_thread_sleep(int secs)
-{
- pth_sleep(secs);
+ pthread_mutex_unlock(&mnod->mutex);
}
static bool
-_pth_eintr(void)
+thread_eintr(void)
{
if (errno == EINTR)
return TRUE;
-/* $Id: mmsmtpd.c,v 1.74 2005/06/27 17:54:52 mmondor Exp $ */
+/* $Id: mmsmtpd.c,v 1.74.2.1 2005/11/24 07:13:39 mmondor Exp $ */
/*
* Copyright (C) 2001-2004, Matthew Mondor
#include <syslog.h>
-#include <pth.h>
#include <signal.h>
#include <time.h>
#include <dirent.h>
#include <ctype.h>
+#include <pthread.h>
+
#include <mmtypes.h>
#include <mmreadcfg.h>
#include <mmfd.h>
MMCOPYRIGHT("@(#) Copyright (c) 2001-2004\n\
\tMatthew Mondor. All rights reserved.\n");
-MMRCSID("$Id: mmsmtpd.c,v 1.74 2005/06/27 17:54:52 mmondor Exp $");
+MMRCSID("$Id: mmsmtpd.c,v 1.74.2.1 2005/11/24 07:13:39 mmondor Exp $");
/* Used for clenv allocation buffering */
static pool_t clenv_pool;
-static pth_mutex_t clenv_lock;
+static pthread_mutex_t clenv_lock = PTHREAD_MUTEX_INITIALIZER;
/* Used for the flood protection cache */
static pool_t hosts_pool;
static hashtable_t hosts_table;
-static pth_mutex_t hosts_lock;
+static pthread_mutex_t hosts_lock = PTHREAD_MUTEX_INITIALIZER;
/* Used for rcpt allocation buffering */
static pool_t rcpt_pool;
-static pth_mutex_t rcpt_lock;
+static pthread_mutex_t rcpt_lock = PTHREAD_MUTEX_INITIALIZER;
/* Pool used to optimize creating/destroying mmfd mutexes */
-static pth_mutex_t mutexes_lock;
+static pthread_mutex_t mutexes_lock = PTHREAD_MUTEX_INITIALIZER;
static pool_t mutexes_pool;
/* For fast command lookup */
static fdfuncs gfdf = {
malloc,
free,
- pth_poll,
- pth_read,
- pth_write,
- pth_sleep,
- pth_usleep,
- _pth_mutex_create,
- _pth_mutex_destroy,
- _pth_mutex_lock,
- _pth_mutex_unlock,
- _pth_thread_yield,
- _pth_eintr
+ poll,
+ read,
+ write,
+ pthread_sleep,
+ pthread_usleep,
+ thread_mutex_create,
+ thread_mutex_destroy,
+ thread_mutex_lock,
+ thread_mutex_unlock,
+ NULL,
+ thread_eintr
};
/*
* Connection to mmrelayd(8) establishment
*/
int relayd_sock = -1;
-pth_mutex_t relayd_lock = PTH_MUTEX_INIT;
+pthread_mutex_t relayd_lock = PTHREAD_MUTEX_INITIALIZER;
{NULL, 0}
};
struct mmsql_threadsupport mmsqlfuncs = {
- _pth_mutex_create,
- _pth_mutex_destroy,
- _pth_mutex_lock,
- _pth_mutex_unlock,
- _pth_thread_yield,
- _pth_thread_sleep
+ thread_mutex_create,
+ thread_mutex_destroy,
+ thread_mutex_lock,
+ thread_mutex_unlock,
+ NULL,
+ pthread_sleep
};
mmstat_t vstat;
- pth_t hosts_table_thread = NULL;
- pth_t mmmail_db_gc_thread = NULL;
- pth_attr_t threadattr;
+ pthread_t hosts_table_thread = NULL;
+ pthread_t mmmail_db_gc_thread = NULL;
+ pthread_attr_t threadattr;
/* Set defaults */
*CONF.CHROOT_DIR = 0;
async_init(afuncs, CONF.ASYNC_PROCESSES, uid, gids, ngids);
/* Things which shouldn't be part of the async pool processes */
- pth_init();
- async_init_pth();
- pth_mutex_init(&clenv_lock);
- pth_mutex_init(&hosts_lock);
- pth_mutex_init(&rcpt_lock);
- pth_mutex_init(&mutexes_lock);
+ async_init_pthread();
/* Allocate necessary pools */
/* Client nodes */
pool_init(&mutexes_pool, "mutexes_pool", malloc, free, NULL, NULL,
sizeof(struct mutexnode),
(16384 * CONF.ALLOC_BUFFERS) / sizeof(struct mutexnode), 0, 0);
+
+ pthread_attr_init(&threadattr);
+ pthread_attr_setdetachstate(&threadattr, 0);
+
/* Rate nodes */
if (CONF.FLOOD_PROTECTION) {
pool_init(&hosts_pool, "hosts_pool", malloc, free, NULL, NULL,
sizeof(hostnode), CONF.FLOOD_CACHE, 1, 1);
hashtable_init(&hosts_table, "hosts_table", CONF.FLOOD_CACHE, 1,
malloc, free, mm_memcmp, mm_memhash32, FALSE);
- threadattr = pth_attr_new();
- pth_attr_set(threadattr, PTH_ATTR_JOINABLE, TRUE);
- hosts_table_thread = pth_spawn(threadattr, hosts_expire_thread, NULL);
+ pthread_create(&hosts_table_thread, &threadattr, hosts_expire_thread,
+ NULL);
}
/* Launch box directories cleaning thread */
- threadattr = pth_attr_new();
- pth_attr_set(threadattr, PTH_ATTR_JOINABLE, TRUE);
- mmmail_db_gc_thread = pth_spawn(threadattr, db_gc_thread, NULL);
+ pthread_create(&mmmail_db_gc_thread, &threadattr, db_gc_thread, NULL);
/* mmstr nodes */
strlist = mmstrinit(malloc, free, 65536 * CONF.ALLOC_BUFFERS);
if (strlist)
mmstrexit();
+ /* XXX
if (hosts_table_thread != NULL) {
pth_abort(hosts_table_thread);
pth_join(hosts_table_thread, NULL);
pth_abort(mmmail_db_gc_thread);
pth_join(mmmail_db_gc_thread, NULL);
}
+ */
if (HASHTABLE_VALID(&command_table))
hashtable_destroy(&command_table, FALSE);
if (POOL_VALID(&command_pool))
*/
{
register rcptnode *rnode;
- register int cnt;
ahash = mm_strhash64(addr);
- cnt = 0;
DLIST_FOREACH(&clenv->rcpt, rnode) {
if (rnode->hash == ahash) {
reason = RCPT_EXISTS;
goto end;
}
- cnt++;
- if (cnt > 64) {
- cnt = 0;
- pth_yield(NULL);
- }
}
}
len = mm_strlen(entry);
valid = TRUE;
- pth_mutex_acquire(&hosts_lock, FALSE, NULL);
+ pthread_mutex_lock(&hosts_lock);
/* First acquire our hostnode, or create it if required */
if ((hnod = (hostnode *)hashtable_lookup(&hosts_table, entry, len + 1))
== NULL) {
clenv->id, LR_POSTS(&hnod->lr), CONF.FLOOD_EXPIRES);
}
}
- pth_mutex_release(&hosts_lock);
+ pthread_mutex_unlock(&hosts_lock);
if (!valid) {
if (CONF.STATFAIL_FLOOD)
register rcptnode *rnode;
reason = RCPT_ERROR;
- pth_mutex_acquire(&rcpt_lock, FALSE, NULL);
+ pthread_mutex_lock(&rcpt_lock);
rnode = (rcptnode *)pool_alloc(&rcpt_pool, FALSE);
- pth_mutex_release(&rcpt_lock);
+ pthread_mutex_unlock(&rcpt_lock);
if (rnode != NULL) {
mm_strcpy(rnode->address, (relay ? foraddr : addr));
mm_strcpy(rnode->foraddress, foraddr);
{
clientenv *clenv;
- pth_mutex_acquire(&clenv_lock, FALSE, NULL);
+ pthread_mutex_lock(&clenv_lock);
clenv = (clientenv *)pool_alloc(&clenv_pool, TRUE);
- pth_mutex_release(&clenv_lock);
+ pthread_mutex_unlock(&clenv_lock);
if (clenv != NULL) {
mmstat_init(&clenv->vstat, TRUE, TRUE);
mmstrfree(clenv->from);
empty_rcpts(&clenv->rcpt);
- pth_mutex_acquire(&clenv_lock, FALSE, NULL);
+ pthread_mutex_lock(&clenv_lock);
pool_free((pnode_t *)clenv);
- pth_mutex_release(&clenv_lock);
+ pthread_mutex_unlock(&clenv_lock);
return (NULL);
}
{
node_t *nod, *tmp;
- pth_mutex_acquire(&rcpt_lock, FALSE, NULL);
+ pthread_mutex_lock(&rcpt_lock);
for (nod = DLIST_TOP(lst); nod != NULL; nod = tmp) {
tmp = DLIST_NEXT(nod);
}
DLIST_INIT(lst);
- pth_mutex_release(&rcpt_lock);
+ pthread_mutex_unlock(&rcpt_lock);
}
if ((mysqlres = mmsql_query(query, mm_strlen(query))) != NULL) {
if ((mysql_num_rows(mysqlres)) > 0) {
char pat[64];
- int cur = 0, max = -1, cnt = 0;
+ int cur = 0, max = -1;
MYSQL_ROW *row;
unsigned long *lengths;
}
}
}
- cnt++;
- if (cnt > 64) {
- cnt = 0;
- pth_yield(NULL);
- }
}
if (max > -1)
res = TRUE;
if ((mysqlres = mmsql_query("SELECT nofrom_pattern FROM nofrom", -1))
!= NULL) {
if ((mysql_num_rows(mysqlres)) > 0) {
- int cnt = 0;
MYSQL_ROW *row;
unsigned long *lengths;
}
}
}
- cnt++;
- if (cnt > 64) {
- cnt = 0;
- pth_yield(NULL);
- }
}
}
mysqlres = mmsql_free_result(mysqlres);
* If we cannot obtain lock, we know that it's already being notified, and
* we don't need to do anything.
*/
- if (pth_mutex_acquire(&relayd_lock, TRUE, NULL) == FALSE)
+ if (pthread_mutex_lock(&relayd_lock) != 0)
return;
/*
* and send it again, but once only.
*/
for (;;) {
- if (pth_write(relayd_sock, "N", 1) != 1) {
+ if (write(relayd_sock, "N", 1) != 1) {
(void) close(relayd_sock);
if ((relayd_sock = do_data_queue_notify_connect()) != -1)
continue;
mmsyslog(0, LOGLEVEL,
"mmrelayd(8) could not be notified (not running?)");
- (void) pth_mutex_release(&relayd_lock);
+ (void) pthread_mutex_unlock(&relayd_lock);
}
/*
mm_memclr(&addr, sizeof(struct sockaddr_un));
(void) mm_strncpy(addr.sun_path, CONF.MMRELAYD_SOCKET_PATH, 100);
addr.sun_family = AF_UNIX;
- if ((pth_connect(fd, (struct sockaddr *)&addr,
+ if ((connect(fd, (struct sockaddr *)&addr,
sizeof(struct sockaddr_un))) == -1) {
(void) close(fd);
fd = -1;
static void *
-_pth_mutex_create(void)
+thread_mutex_create(void)
{
struct mutexnode *mnod;
- pth_mutex_acquire(&mutexes_lock, FALSE, NULL);
+ pthread_mutex_lock(&mutexes_lock);
mnod = (struct mutexnode *)pool_alloc(&mutexes_pool, FALSE);
- pth_mutex_release(&mutexes_lock);
+ pthread_mutex_unlock(&mutexes_lock);
if (mnod != NULL)
- pth_mutex_init(&mnod->mutex);
+ pthread_mutex_init(&mnod->mutex, NULL);
return ((void *)mnod);
}
static void *
-_pth_mutex_destroy(void *mtx)
+thread_mutex_destroy(void *mtx)
{
- /* struct mutexnode *mnod = mtx; */
+ struct mutexnode *mnod = mtx;
- /* pth_mutex_destroy(&mnod->mutex); */
- pth_mutex_acquire(&mutexes_lock, FALSE, NULL);
+ pthread_mutex_destroy(&mnod->mutex);
+ pthread_mutex_lock(&mutexes_lock);
pool_free(mtx);
- pth_mutex_release(&mutexes_lock);
+ pthread_mutex_unlock(&mutexes_lock);
return (NULL);
}
static void
-_pth_mutex_lock(void *mtx)
+thread_mutex_lock(void *mtx)
{
struct mutexnode *mnod = mtx;
- pth_mutex_acquire(&mnod->mutex, FALSE, NULL);
+ pthread_mutex_lock(&mnod->mutex);
}
static void
-_pth_mutex_unlock(void *mtx)
+thread_mutex_unlock(void *mtx)
{
struct mutexnode *mnod = mtx;
- pth_mutex_release(&mnod->mutex);
-}
-
-
-static void
-_pth_thread_yield(void)
-{
- pth_yield(NULL);
-}
-
-
-static void
-_pth_thread_sleep(int secs)
-{
- pth_sleep(secs);
+ pthread_mutex_unlock(&mnod->mutex);
}
static bool
-_pth_eintr(void)
+thread_eintr(void)
{
if (errno == EINTR)
return TRUE;
/* Set the initial timeout to the maximum allowed */
data.soonest = CONF.FLOOD_EXPIRES * 60;
- data.cnt = 0;
for (;;) {
/* Sleep until it is known that at least one node expired */
- pth_sleep((unsigned int)data.soonest);
+ pthread_sleep(data.soonest);
/* Tell our iterator function the current time and the maximum
* allowed time to wait to
*/
/* Lock hosts_table, expunge expired nodes and set data.soonest to the
* time of the soonest next expireing node
*/
- pth_mutex_acquire(&hosts_lock, FALSE, NULL);
+ pthread_mutex_lock(&hosts_lock);
if (HASHTABLE_NODES(&hosts_table) > 0)
hashtable_iterate(&hosts_table, hosts_expire_thread_iterator,
&data);
- pth_mutex_release(&hosts_lock);
+ pthread_mutex_unlock(&hosts_lock);
}
/* NOTREACHED */
- pth_exit(NULL);
+ pthread_exit(NULL);
return NULL;
}
data->soonest = rem;
}
- /* If the cache is big, prevent from interfering with other threads */
- if ((data->cnt++) == 64) {
- data->cnt = 0;
- pth_yield(NULL);
- }
-
return TRUE;
}
for (rounds = 1; ; rounds++) {
MYSQL_RES *mysqlres;
- (void) pth_sleep(60);
+ (void) pthread_sleep(60);
/*
* Perform dangling mailbox directories cleanup
}
/* NOTREACHED */
- pth_exit(NULL);
+ pthread_exit(NULL);
return NULL;
}
char dirpath[256], filepath[256];
DIR *dir;
struct dirent ent, *res;
- int count;
/*
* Now <path> holds the actual directory to delete mail from. We know
return;
}
- count = 1;
while (readdir_r(dir, &ent, &res) == 0 && res == &ent) {
(void) snprintf(filepath, 255, "%s/%s", dirpath, ent.d_name);
if (unlink(filepath) != 0)
syslog(LOG_NOTICE, "db_gc_thread_delete(%s) - unlink(%s) == %s",
addr, filepath, strerror(errno));
- if (++count == 64) {
- count = 0;
- (void) pth_yield(NULL);
- }
}
if (rmdir(dirpath) != 0)
syslog(LOG_NOTICE, "db_gc_thread_delete(%s) - rmdir(%s) == %s",