Skip to content

Commit 4271b2b

Browse files
authored
Merge pull request #1354 from martin-frbg/shmem
Try to handle shmget or shmat failing
2 parents cc26cdc + 148493d commit 4271b2b

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

driver/others/init.c

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ static inline int is_dead(int id) {
631631
return shmctl(id, IPC_STAT, &ds);
632632
}
633633

634-
static void open_shmem(void) {
634+
static int open_shmem(void) {
635635

636636
int try = 0;
637637

@@ -657,30 +657,42 @@ static void open_shmem(void) {
657657
} while ((try < 10) && (shmid == -1));
658658

659659
if (shmid == -1) {
660-
fprintf(stderr, "GotoBLAS : Can't open shared memory. Terminated.\n");
661-
exit(1);
660+
perror ("Obtaining shared memory segment failed in open_shmem");
661+
return (1);
662662
}
663663

664664
if (shmid != -1) {
665-
if ( (common = shmat(shmid, NULL, 0)) == (void*)-1) perror ("Attaching shared memory segment");
665+
if ( (common = shmat(shmid, NULL, 0)) == (void*)-1) {
666+
perror ("Attaching shared memory segment failed in open_shmem");
667+
return (1);
668+
}
666669
}
667670
#ifdef DEBUG
668671
fprintf(stderr, "Shared Memory id = %x Address = %p\n", shmid, common);
669672
#endif
670-
673+
return (0);
671674
}
672675

673-
static void create_pshmem(void) {
676+
static int create_pshmem(void) {
674677

675678
pshmid = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
676679

677-
paddr = shmat(pshmid, NULL, 0);
678-
679-
shmctl(pshmid, IPC_RMID, 0);
680+
if (pshmid == -1) {
681+
perror ("Obtaining shared memory segment failed in create_pshmem");
682+
return(1);
683+
}
684+
685+
if ( (paddr = shmat(pshmid, NULL, 0)) == (void*)-1) {
686+
perror ("Attaching shared memory segment failed in create_pshmem");
687+
return (1);
688+
}
689+
690+
if (shmctl(pshmid, IPC_RMID, 0) == -1) return (1);
680691

681692
#ifdef DEBUG
682693
fprintf(stderr, "Private Shared Memory id = %x Address = %p\n", pshmid, paddr);
683694
#endif
695+
return(0);
684696
}
685697

686698
static void local_cpu_map(void) {
@@ -808,17 +820,23 @@ void gotoblas_affinity_init(void) {
808820
return;
809821
}
810822

811-
create_pshmem();
812-
813-
open_shmem();
814-
823+
if (create_pshmem() != 0) {
824+
disable_mapping = 1;
825+
return;
826+
}
827+
828+
if (open_shmem() != 0) {
829+
disable_mapping = 1;
830+
return;
831+
}
832+
815833
while ((common -> lock) && (common -> magic != SH_MAGIC)) {
816834
if (is_dead(common -> shmid)) {
817835
common -> lock = 0;
818836
common -> shmid = 0;
819837
common -> magic = 0;
820838
} else {
821-
sched_yield();
839+
YIELDING;
822840
}
823841
}
824842

0 commit comments

Comments
 (0)