~ubuntu-branches/ubuntu/oneiric/pulseaudio/oneiric

« back to all changes in this revision

Viewing changes to src/pulsecore/core-util.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2007-12-04 00:56:08 UTC
  • mto: (1.15.1 sid)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20071204005608-3lzrrrpxi186kgx4
Tags: upstream-0.9.8
ImportĀ upstreamĀ versionĀ 0.9.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: core-util.c 1987 2007-10-29 21:19:05Z lennart $ */
 
1
/* $Id: core-util.c 2019 2007-11-04 14:11:53Z lennart $ */
2
2
 
3
3
/***
4
4
  This file is part of PulseAudio.
512
512
    return b;
513
513
}
514
514
 
515
 
/* Make the current thread a realtime thread*/
516
 
void pa_make_realtime(void) {
 
515
/* Make the current thread a realtime thread, and acquire the highest
 
516
 * rtprio we can get that is less or equal the specified parameter. If
 
517
 * the thread is already realtime, don't do anything. */
 
518
int pa_make_realtime(int rtprio) {
517
519
 
518
520
#ifdef _POSIX_PRIORITY_SCHEDULING
519
521
    struct sched_param sp;
524
526
 
525
527
    if ((r = pthread_getschedparam(pthread_self(), &policy, &sp)) != 0) {
526
528
        pa_log("pthread_getschedgetparam(): %s", pa_cstrerror(r));
527
 
        return;
528
 
    }
529
 
 
530
 
    sp.sched_priority = 1;
 
529
        return -1;
 
530
    }
 
531
 
 
532
    if (policy == SCHED_FIFO && sp.sched_priority >= rtprio) {
 
533
        pa_log_info("Thread already being scheduled with SCHED_FIFO with priority %i.", sp.sched_priority);
 
534
        return 0;
 
535
    }
 
536
 
 
537
    sp.sched_priority = rtprio;
531
538
    if ((r = pthread_setschedparam(pthread_self(), SCHED_FIFO, &sp)) != 0) {
 
539
 
 
540
        while (sp.sched_priority > 1) {
 
541
            sp.sched_priority --;
 
542
 
 
543
            if ((r = pthread_setschedparam(pthread_self(), SCHED_FIFO, &sp)) == 0) {
 
544
                pa_log_info("Successfully enabled SCHED_FIFO scheduling for thread, with priority %i, which is lower than the requested %i.", sp.sched_priority, rtprio);
 
545
                return 0;
 
546
            }
 
547
        }
 
548
 
532
549
        pa_log_warn("pthread_setschedparam(): %s", pa_cstrerror(r));
533
 
        return;
 
550
        return -1;
534
551
    }
535
552
 
536
 
    pa_log_info("Successfully enabled SCHED_FIFO scheduling for thread.");
 
553
    pa_log_info("Successfully enabled SCHED_FIFO scheduling for thread, with priority %i.", sp.sched_priority);
 
554
    return 0;
 
555
#else
 
556
    return -1;
537
557
#endif
538
 
 
539
558
}
540
559
 
541
 
#define NICE_LEVEL (-11)
542
 
 
543
 
/* Raise the priority of the current process as much as possible and
544
 
sensible: set the nice level to -15.*/
545
 
void pa_raise_priority(void) {
 
560
/* Raise the priority of the current process as much as possible that
 
561
 * is <= the specified nice level..*/
 
562
int pa_raise_priority(int nice_level) {
546
563
 
547
564
#ifdef HAVE_SYS_RESOURCE_H
548
 
    if (setpriority(PRIO_PROCESS, 0, NICE_LEVEL) < 0)
 
565
    if (setpriority(PRIO_PROCESS, 0, nice_level) < 0) {
 
566
        int n;
 
567
 
 
568
        for (n = nice_level+1; n < 0; n++) {
 
569
 
 
570
            if (setpriority(PRIO_PROCESS, 0, n) == 0) {
 
571
                pa_log_info("Successfully acquired nice level %i, which is lower than the requested %i.", n, nice_level);
 
572
                return 0;
 
573
            }
 
574
        }
 
575
 
549
576
        pa_log_warn("setpriority(): %s", pa_cstrerror(errno));
550
 
    else
551
 
        pa_log_info("Successfully gained nice level %i.", NICE_LEVEL);
 
577
        return -1;
 
578
    }
 
579
 
 
580
    pa_log_info("Successfully gained nice level %i.", nice_level);
552
581
#endif
553
582
 
554
583
#ifdef OS_IS_WIN32
555
 
    if (!SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS))
556
 
        pa_log_warn("SetPriorityClass() failed: 0x%08X", GetLastError());
557
 
    else
558
 
        pa_log_info("Successfully gained high priority class.");
 
584
    if (nice_level < 0) {
 
585
        if (!SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS)) {
 
586
            pa_log_warn("SetPriorityClass() failed: 0x%08X", GetLastError());
 
587
            return .-1;
 
588
        } else
 
589
            pa_log_info("Successfully gained high priority class.");
 
590
    }
559
591
#endif
 
592
 
 
593
    return 0;
560
594
}
561
595
 
562
596
/* Reset the priority to normal, inverting the changes made by
563
 
 * pa_raise_priority() */
 
597
 * pa_raise_priority() and pa_make_realtime()*/
564
598
void pa_reset_priority(void) {
 
599
#ifdef HAVE_SYS_RESOURCE_H
 
600
    struct sched_param sp;
 
601
 
 
602
    setpriority(PRIO_PROCESS, 0, 0);
 
603
 
 
604
    memset(&sp, 0, sizeof(sp));
 
605
    pa_assert_se(pthread_setschedparam(pthread_self(), SCHED_OTHER, &sp) == 0);
 
606
#endif
 
607
 
565
608
#ifdef OS_IS_WIN32
566
609
    SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
567
610
#endif
568
 
 
569
 
#ifdef HAVE_SYS_RESOURCE_H
570
 
    setpriority(PRIO_PROCESS, 0, 0);
571
 
#endif
572
611
}
573
612
 
574
613
/* Try to parse a boolean string value.*/
1523
1562
        }
1524
1563
 
1525
1564
        if ((size_t) n < l-1) {
1526
 
            c[l-1] = 0;
 
1565
            c[n] = 0;
1527
1566
            return c;
1528
1567
        }
1529
1568