author Marco Castelluccio <>
Fri, 18 Nov 2011 17:00:37 +1300
changeset 82066 215593486382ba9710ee70056a94f44b1ab846c7
parent 80725 78e97de08278a99d5aa57373d847cb2892e26d63
child 82502 18019396a173d42e40b9504241ea800c6aae88e1
permissions -rw-r--r--
Bug 699258 - Get skia backend compiling and running on Windows. r=mattwoodrow

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 * The Original Code is code.
 * The Initial Developer of the Original Code is
 * Mozilla Foundation.
 * Portions created by the Initial Developer are Copyright (C) 2011
 * the Initial Developer. All Rights Reserved.
 * Contributor(s):
 *   Benoit Girard <>
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * ***** END LICENSE BLOCK ***** */

#include <pthread.h>
#include "nscore.h"
#include "base/atomicops.h"

// TODO Merge into Sampler.h

extern pthread_key_t pkey_stack;
extern pthread_key_t pkey_ticker;

#define SAMPLER_INIT() mozilla_sampler_init();
#define SAMPLER_DEINIT() mozilla_sampler_deinit();
#define SAMPLE_CHECKPOINT(name_space, info) mozilla::SamplerStackFrameRAII only_one_sampleraii_per_scope(FULLFUNCTION, name_space "::" info);
#define SAMPLE_MARKER(info) mozilla_sampler_add_marker(info);

// STORE_SEQUENCER: Because signals can interrupt our profile modification
//                  we need to make stores are not re-ordered by the compiler
//                  or hardware to make sure the profile is consistent at
//                  every point the signal can fire.
// TODO Is there something cheaper that will prevent
//      memory stores from being reordered
// Uses: pLinuxKernelMemoryBarrier
# define STORE_SEQUENCER() base::subtle::MemoryBarrier();
# define STORE_SEQUENCER() asm volatile("" ::: "memory");
# error "Memory clobber not supported for your platform."

// Returns a handdle to pass on exit. This can check that we are popping the
// correct callstack.
inline void* mozilla_sampler_call_enter(const char *aInfo);
inline void  mozilla_sampler_call_exit(void* handle);
inline void  mozilla_sampler_add_marker(const char *aInfo);

void mozilla_sampler_init();

namespace mozilla {

class NS_STACK_CLASS SamplerStackFrameRAII {
  SamplerStackFrameRAII(const char *aFuncName, const char *aInfo) {
    mHandle = mozilla_sampler_call_enter(aInfo);
  ~SamplerStackFrameRAII() {
  void* mHandle;

} //mozilla

// the SamplerStack members are read by signal
// handlers, so the mutation of them needs to be signal-safe.
struct Stack
    : mStackPointer(0)
    , mMarkerPointer(0)
    , mDroppedStackEntries(0)
    , mQueueClearMarker(false)
  { }

  void addMarker(const char *aMarker)
    if (mQueueClearMarker) {
    if (!aMarker) {
      return; //discard
    if (mMarkerPointer == 1024) {
      return; //array full, silently drop
    mMarkers[mMarkerPointer] = aMarker;

  // called within signal. Function must be reentrant
  const char* getMarker(int aMarkerId)
    if (mQueueClearMarker) {
    if (aMarkerId >= mMarkerPointer) {
      return NULL;
    return mMarkers[aMarkerId];

  // called within signal. Function must be reentrant
  void clearMarkers()
    mMarkerPointer = 0;
    mQueueClearMarker = false;

  void push(const char *aName)
    if (mStackPointer >= 1024) {

    // Make sure we increment the pointer after the name has
    // been written such that mStack is always consistent.
    mStack[mStackPointer] = aName;
    // Prevent the optimizer from re-ordering these instructions
  void pop()
    if (mDroppedStackEntries > 0) {
    } else {
  bool isEmpty()
    return mStackPointer == 0;

  // Keep a list of active checkpoints
  const char *mStack[1024];
  // Keep a list of active markers to be applied to the next sample taken
  const char *mMarkers[1024];
  sig_atomic_t mStackPointer;
  sig_atomic_t mMarkerPointer;
  sig_atomic_t mDroppedStackEntries;
  // We don't want to modify _markers from within the signal so we allow
  // it to queue a clear operation.
  sig_atomic_t mQueueClearMarker;

inline void* mozilla_sampler_call_enter(const char *aInfo)
  Stack *stack = (Stack*)pthread_getspecific(pkey_stack);
  if (!stack) {
    return stack;

  // The handle is meant to support future changes
  // but for now it is simply use to save a call to
  // pthread_getspecific on exit. It also supports the
  // case where the sampler is initialized between
  // enter and exit.
  return stack;

inline void mozilla_sampler_call_exit(void *aHandle)
  if (!aHandle)

  Stack *stack = (Stack*)aHandle;

inline void mozilla_sampler_add_marker(const char *aMarker)
  Stack *stack = (Stack*)pthread_getspecific(pkey_stack);
  if (!stack) {