1// Copyright 2018 The Fuchsia Authors
2//
3// Use of this source code is governed by a MIT-style
4// license that can be found in the LICENSE file or at
5// https://opensource.org/licenses/MIT
6
7#include <object/profile_dispatcher.h>
8
9#include <err.h>
10
11#include <fbl/alloc_checker.h>
12#include <fbl/ref_ptr.h>
13
14#include <object/thread_dispatcher.h>
15
16#include <zircon/rights.h>
17
18zx_status_t validate_profile(const zx_profile_info_t& info) {
19    if (info.type != ZX_PROFILE_INFO_SCHEDULER)
20        return ZX_ERR_NOT_SUPPORTED;
21    if ((info.scheduler.priority < LOWEST_PRIORITY) ||
22        (info.scheduler.priority  > HIGHEST_PRIORITY))
23        return ZX_ERR_INVALID_ARGS;
24    return ZX_OK;
25}
26
27zx_status_t ProfileDispatcher::Create(const zx_profile_info_t& info,
28                                      fbl::RefPtr<Dispatcher>* dispatcher,
29                                      zx_rights_t* rights) {
30    auto status = validate_profile(info);
31    if (status != ZX_OK)
32        return status;
33
34    fbl::AllocChecker ac;
35    auto disp = new (&ac) ProfileDispatcher(info);
36    if (!ac.check())
37        return ZX_ERR_NO_MEMORY;
38
39    *rights = ZX_DEFAULT_PROFILE_RIGHTS;
40    *dispatcher = fbl::AdoptRef<Dispatcher>(disp);
41    return ZX_OK;
42}
43
44ProfileDispatcher::ProfileDispatcher(const zx_profile_info_t& info)
45    : info_(info) {}
46
47ProfileDispatcher::~ProfileDispatcher() {
48}
49
50zx_status_t ProfileDispatcher::ApplyProfile(fbl::RefPtr<ThreadDispatcher> thread) {
51    // At the moment, the only thing we support is the priority.
52    return thread->SetPriority(info_.scheduler.priority);
53}
54