diff --git a/library/DFProcess-linux-SHM.cpp b/library/DFProcess-linux-SHM.cpp index f8a83275d..c1f8b954a 100644 --- a/library/DFProcess-linux-SHM.cpp +++ b/library/DFProcess-linux-SHM.cpp @@ -215,6 +215,7 @@ void SHMProcess::Private::FreeLocks() { close(suspend_lock); locked = false; + suspended = false; suspend_lock = -1; } } @@ -327,8 +328,9 @@ SHMProcess::SHMProcess(uint32_t PID, vector< memory_info* >& known_versions) } if(!bridgeOK) { - detach(); throw Error::SHMVersionMismatch(); + detach(); + return; } @@ -473,11 +475,13 @@ bool SHMProcess::suspend() if(D_SHMCMD == CORE_RUN) { //fprintf(stderr,"%d invokes step\n",d->attachmentIdx); + // wait for the next window D_SHMCMD = CORE_STEP; } else { //fprintf(stderr,"%d invokes suspend\n",d->attachmentIdx); + // lock now D_SHMCMD = CORE_SUSPEND; } //fprintf(stderr,"waiting for lock\n"); @@ -579,24 +583,22 @@ bool SHMProcess::attach() /* * Attach the segment */ - if ((d->shm_addr = (char *) shmat(d->shm_ID, NULL, 0)) != (char *) -1) + if ((d->shm_addr = (char *) shmat(d->shm_ID, NULL, 0)) == (char *) -1) + { + d->FreeLocks(); + cerr << "can't attach segment" << endl; + return false; + } + d->attached = true; + if(!suspend()) { - d->attached = true; - if(suspend()) - { - d->suspended = true; - g_pProcess = this; - return true; - } - d->attached = false; - cerr << "unable to suspend" << endl; shmdt(d->shm_addr); d->FreeLocks(); + cerr << "unable to suspend" << endl; return false; } - cerr << "unable to attach" << endl; - d->FreeLocks(); - return false; + g_pProcess = this; + return true; } bool SHMProcess::detach() diff --git a/shmserver/mod-core.cpp b/shmserver/mod-core.cpp index 7b5f0a1ab..3bc27b12e 100644 --- a/shmserver/mod-core.cpp +++ b/shmserver/mod-core.cpp @@ -51,7 +51,7 @@ bool useYield = 0; int currentClient = -1; #define SHMHDR ((shm_core_hdr *)shm) -#define SHMCMDPP ((shm_core_hdr *) shm)->cmd[currentClient].pingpong +#define SHMCMD ((uint32_t *)shm )[currentClient] #define SHMDATA(type) ((type *)(shm + SHM_HEADER)) void ReadRaw (void * data) @@ -264,7 +264,7 @@ void SHM_Act (void) else { full_barrier - SHMCMDPP = CORE_RUNNING; + SHMCMD = CORE_RUNNING; fprintf(stderr,"dfhack: Broke out of loop, other process disappeared.\n"); } } @@ -276,7 +276,7 @@ void SHM_Act (void) // this is very important! copying two words separately from the command variable leads to inconsistency. // Always copy the thing in one go. // Also, this whole SHM thing probably only works on intel processors - atomic = *(uint32_t *) (shm + 4*currentClient); //SHMHDR->cmd[currentClient]; + atomic = SHMCMD; full_barrier DFPP_module & mod = module_registry[ ((shm_cmd)atomic).parts.module ]; @@ -303,7 +303,7 @@ void SHM_Act (void) currentClient,((shm_cmd)atomic).parts.module,((shm_cmd)atomic).parts.command, cmd._function); fprintf(stderr, "%s\n",cmd.name.c_str()); // FIXME: WHAT HAPPENS WHEN A 'NEXTSTATE' IS FROM A DIFFERENT MODULE THAN 'CORE'? Yeah. It doesn't work. - SHMCMDPP = cmd.nextState; + SHMCMD = cmd.nextState; fprintf(stderr, "Server set %d\n",cmd.nextState); } full_barrier diff --git a/shmserver/shms-linux.cpp b/shmserver/shms-linux.cpp index 3e79753bf..aa157c1be 100644 --- a/shmserver/shms-linux.cpp +++ b/shmserver/shms-linux.cpp @@ -196,7 +196,7 @@ void SHM_Init ( void ) // make sure we don't stall or do crazy stuff for(int i = 0; i < SHM_MAX_CLIENTS;i++) { - ((shm_cmd *)shm)[i].pingpong = CORE_RUNNING; + ((uint32_t *)shm)[i] = CORE_RUNNING; } InitModules(); }