| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <new> |
| 4 | |
| 5 | #include "seatac.H" |
| 6 | |
| 7 | // Define this to print a message whenever a sequence is loaded. |
| 8 | // Useful for testing the loader with large sequences (scaffolds, |
| 9 | // chromosomes). |
| 10 | // |
| 11 | //#define VERBOSE_LOADER |
| 12 | |
| 13 | #ifdef TRUE64BIT |
| 14 | char const *loadDesc = "WARNING: Loader ran dry. Increasing limit to %u sequences, decreasing sleep to %f.\n"; |
| 15 | #else |
| 16 | char const *loadDesc = "WARNING: Loader ran dry. Increasing limit to %lu sequences, decreasing sleep to %f.\n"; |
| 17 | #endif |
| 18 | |
| 19 | void* |
| 20 | loaderThread(void *) { |
| 21 | uint32 waterLevel = 0; |
| 22 | seqInCore *B = 0L; |
| 23 | bool slept = false; |
| 24 | |
| 25 | while (inputHead < numberOfQueries) { |
| 26 | |
| 27 | // We fill the input as fast as we can, up to the high water |
| 28 | // mark, then we take a little snooze to let the workers catch up. |
| 29 | // |
| 30 | pthread_mutex_lock(&inputTailMutex); |
| 31 | waterLevel = inputHead - inputTail; |
| 32 | pthread_mutex_unlock(&inputTailMutex); |
| 33 | |
| 34 | // Warn if we're too small. |
| 35 | // |
| 36 | if ((slept) && (waterLevel <= 1)) { |
| 37 | |
| 38 | uint32 i = (uint32) (0.1 * config._loaderHighWaterMark); |
| 39 | if (i == 0) |
| 40 | i = 1; |
| 41 | config._loaderHighWaterMark += i; |
| 42 | |
| 43 | config.setTime(&config._loaderSleep, |
| 44 | 0.9 * ((double)config._loaderSleep.tv_sec + (double)config._loaderSleep.tv_nsec * 1e-9)); |
| 45 | |
| 46 | if (config._loaderWarnings) |
| 47 | fprintf(stderr, loadDesc, |
| 48 | config._loaderHighWaterMark, |
| 49 | ((double)config._loaderSleep.tv_sec + (double)config._loaderSleep.tv_nsec * 1e-9)); |
| 50 | } |
| 51 | |
| 52 | // Sleep, if we need to, otherwise, get the next sequence and |
| 53 | // push it onto the input list at inputHead. This alloc is |
| 54 | // deleted by the output thread. |
| 55 | // |
| 56 | if (waterLevel >= config._loaderHighWaterMark) { |
| 57 | slept = true; |
| 58 | nanosleep(&config._loaderSleep, 0L); |
| 59 | } else { |
| 60 | slept = false; |
| 61 | |
| 62 | #ifdef VERBOSE_LOADER |
| 63 | fprintf(stderr, "Loading sequence %u (tail = %u)\n", inputHead, inputTail); |
| 64 | #endif |
| 65 | |
| 66 | try { |
| 67 | B = qsFASTA->getSequenceInCore(); |
| 68 | } catch (std::bad_alloc) { |
| 69 | fprintf(stderr, "loaderThread()-- Failed to load next query sequence\ncaught bad_alloc in %s at line %d\n", __FILE__, __LINE__); |
| 70 | exit(1); |
| 71 | } |
| 72 | |
| 73 | pthread_mutex_lock(&inputTailMutex); |
| 74 | input[inputHead] = B; |
| 75 | inputHead++; |
| 76 | pthread_mutex_unlock(&inputTailMutex); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return(0L); |
| 81 | } |