Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Pancake2/PancakeWorkers.c
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
64 lines (48 sloc)
1.4 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "PancakeWorkers.h" | |
#include "PancakeLogger.h" | |
/* Forward declaration */ | |
STATIC void PancakeInternalCommunicationEvent(PancakeSocket *sock); | |
PANCAKE_API UByte PancakeRunWorker(PancakeWorker *worker) { | |
pid_t pid; | |
Int32 sockets[2]; | |
// Create sockets for internal communication | |
if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) { | |
PancakeLoggerFormat(PANCAKE_LOGGER_ERROR, 0, "Can't create sockets for internal communication: %s", strerror(errno)); | |
return 0; | |
} | |
worker->masterSocket = sockets[0]; | |
worker->workerSocket.fd = sockets[1]; | |
worker->workerSocket.flags = 0; | |
worker->workerSocket.onRead = PancakeInternalCommunicationEvent; | |
// Fork child from master | |
pid = fork(); | |
if(pid == -1) { | |
PancakeLoggerFormat(PANCAKE_LOGGER_ERROR, 0, "Can't fork: %s", strerror(errno)); | |
return 0; | |
} else if(pid) { | |
/* Master */ | |
worker->pid = pid; | |
return 1; | |
} else { | |
/* Child */ | |
worker->pid = getpid(); | |
PancakeCurrentWorker = worker; | |
// Register communication socket | |
PancakeNetworkAddReadSocket(&worker->workerSocket); | |
PancakeDebug { | |
PancakeLoggerFormat(PANCAKE_LOGGER_SYSTEM, 0, "PID: %i", worker->pid); | |
} | |
worker->run(); | |
return 2; | |
} | |
} | |
STATIC void PancakeInternalCommunicationEvent(PancakeSocket *sock) { | |
UByte instruction; | |
read(sock->fd, &instruction, 1); | |
switch(instruction) { | |
default: | |
case PANCAKE_WORKER_GRACEFUL_SHUTDOWN_INT: | |
PancakeDoShutdown = 1; | |
break; | |
} | |
} |