Consider your SPI a shared resource (critical section), which must be mutually exclusive.
Here is the general scheme for preventing a context-switch during SPI-access operations:
static pthread_mutex_t _mutex;
void SPI_init()
{
...
pthread_mutex_init(&_mutex);
...
}
void SPI_read(...)
{
pthread_mutex_lock(&_mutex);
...
pthread_mutex_unlock(&_mutex);
}
void SPI_write(...)
{
pthread_mutex_lock(&_mutex);
...
pthread_mutex_unlock(&_mutex);
}