Advertisement
NovaYoshi

ipc.c

Sep 17th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. /*
  2.  * SparklesChat
  3.  *
  4.  * Copyright (C) 2014-2015 NovaSquirrel
  5.  *
  6.  * This program is free software: you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License as
  8.  * published by the Free Software Foundation; either version 2 of the
  9.  * License, or (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19. #include "chat.h"
  20.  
  21. void IPC_New(IPC_Holder *IPC) {
  22.   IPC->Message = NULL;
  23.   IPC->Lock = SDL_CreateMutex();
  24.   SDL_AtomicSet(&IPC->Ready, 0);
  25. }
  26.  
  27. void IPC_Free(IPC_Holder *IPC) {
  28.   IPC_Message *Msg = IPC->Message;
  29.   while(Msg) {
  30.     IPC_Message *Next = Msg->Next;
  31.     free(Msg->Data);
  32.     free(Msg);
  33.     Msg = Next;
  34.   }
  35.   SDL_DestroyMutex(IPC->Lock);
  36. }
  37.  
  38. void IPC_Write(IPC_Holder *IPC, const char *Text) {
  39.   SDL_LockMutex(IPC->Lock);
  40.   char *Clone = strdup(Text);
  41.   if(!Clone)
  42.     abort();
  43.  
  44.   // find where to add the new message
  45.   IPC_Message *AddTo = IPC->Message;
  46.   while(AddTo && AddTo->Next)
  47.     AddTo = AddTo->Next;
  48.  
  49.   // make a new message and stick it on the end
  50.   IPC_Message *New = (IPC_Message*)malloc(sizeof(IPC_Message));
  51.   if(New) {
  52.     if(AddTo)
  53.       AddTo->Next = New;
  54.     else
  55.       IPC->Message = New;
  56.     New->Data = Clone;
  57.     New->Next = NULL;
  58.   } else
  59.     free(Clone);
  60.  
  61.   SDL_UnlockMutex(IPC->Lock);
  62.   SDL_AtomicAdd(&IPC->Ready, 1);
  63.   return;
  64. }
  65.  
  66. void IPC_WriteF(IPC_Holder *IPC, const char *format, ...) {
  67.   va_list args;
  68.   char *buf;
  69.   va_start(args, format);
  70.   buf = strdup_vprintf(format, args);
  71.   va_end(args);
  72.   IPC_Write(IPC, buf);
  73.   free(buf);
  74. }
  75.  
  76. char *IPC_Read(int Timeout, int Count, ...) {
  77.   int i;
  78.   IPC_Holder *IPC[Count];
  79.  
  80.   va_list ap;
  81.   va_start(ap, Count);
  82.   for(i=0; i<Count; i++)
  83.     IPC[i] = va_arg(ap, IPC_Holder*);
  84.   va_end(ap);
  85.  
  86.   // to do: use a condition variable
  87.   int Delay = Timeout / 10;
  88.   for(int Tries = 0; Tries < 10; Tries++) {
  89.     for(i=0; i<Count; i++)
  90.       if(SDL_AtomicGet(&IPC[i]->Ready)) {
  91.         SDL_LockMutex(IPC[i]->Lock);
  92.         char *String = 0;
  93.         if(IPC[i]->Message) {
  94.           String = IPC[i]->Message->Data;
  95.           IPC_Message *Next = IPC[i]->Message->Next;
  96.           free(IPC[i]->Message);
  97.           IPC[i]->Message = Next;
  98.         }
  99.         SDL_UnlockMutex(IPC[i]->Lock);
  100.         SDL_AtomicAdd(&IPC[i]->Ready, -1);
  101.         return String;
  102.       }
  103.     SDL_Delay(Delay);
  104.   }
  105.   return NULL;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement